C++处理我自己的Exception类
C++ Handling My own Exception class
我在显示子类中的一些字符串时遇到问题。我试着用一个函数来做这件事,但我不确定为什么我没有得到这些字符串的内容。
class Employee{
string FN, LN, JT;
double Income;
public:
char const *getters(){
return FN.data(), LN.data(), JT.data(); //=========>getting the content of strings
}
virtual char const *getAccess()=0;
Employee(char const *fn, char const *ln, char const *jt, double inc){
if(fn==0) throw Exception(1, "Sorry, First Name is Null");
if(ln==0) throw Exception(2, "Sorry, Last Name is Null");
if(jt==0) throw Exception(3, "Sorry Job Title is Null");
if(inc<=0) throw Exception(4, "Sorry, The Income is Null");
FN=fn;
LN=ln;
JT=jt;
Income=inc;
}
};
class Programmer: public Employee{
public:
Programmer(char const *fn, char const *ln, double inc):
Employee(fn,ln,"Programmer", inc)
{}
char const *getAccess(){
return "You have access to Meeting Room + Development Office";
}
};
//=========The Main============
int main(){
Employee *acc[3];
try{
acc[0]=new Programmer("Juan", "Villalobos", 60000);
acc[1]=new Director("Jorge", "Villabuena", 70000);
acc[2]=new ProdSupport("Pedro", "Villasmil", 80000);
for(int i=0; i<3; i++){
cout << acc[i]->getters() << endl; //=============>Displaying the strings
cout << acc[i]->getAccess() << endl;
}
} catch(Exception acc){
cout << "Err:" << acc.getErrCode() << " Mess:" << acc.getErrMess() << endl;
}
return 0;
}
所以,我猜我的函数并没有完成我想要的,即显示名字和姓氏。我做错了什么?
我不明白混合char*
和string
的意义。更喜欢后者。
这确实编译了
char const *getters(){
return FN.data(), LN.data(), JT.data();
}
但你可能想要的是
char const *getters(){
return (FN + LN + JT).data();
}
我会这样重写你的程序:
class Employee{
string FN, LN, JT;
double Income;
public:
string getters(){
return FN + " " + LN + " " + JT;
}
virtual string getAccess()=0;
Employee(string const &fn, string const &ln, string const &jt, double inc) :
FN(fn), LN(ln), JT(jt), Income(inc)
{
}
};
class Programmer: public Employee{
public:
Programmer(string const &fn, string const &ln, double inc):
Employee(fn,ln,"Programmer", inc)
{}
string getAccess(){
return "You have access to Meeting Room + Development Office";
}
};
//=========The Main============
int main()
{
std::vector<Employee> acc;
acc.push_back(Programmer("Juan", "Villalobos", 60000));
acc.push_back(Director("Jorge", "Villabuena", 70000));
acc.push_back(ProdSupport("Pedro", "Villasmil", 80000));
for(size_t i=0; i<acc.size(); i++){
cout << acc[i].getters() << endl;
cout << acc[i].getAccess() << endl;
}
return 0;
}
,
逗号运算符的结果是右边的值,因此return FN.data(), LN.data(), JT.data();
实际上与return JT.data();
相同,这就是您所看到的。
要做你正在尝试的事情,请尝试以下操作:
std::vector<std::string> getValues() const {
std::vector<std::string> arr(3);
arr.push_back(FN);
arr.push_back(LN);
arr.push_back(JT);
return arr;
}
std::vector<std::string> arr = acc[i]->getValues();
for (std::vector<std::string>::const_iterator iter = arr.begin(), end = arr.end(); iter != end; ++iter) {
cout << *iter << " ";
}
cout << endl;
或者,将cout
逻辑移动到类本身中:
void displayValues() const {
cout << FN << " " << LN << " " << JT << endl;
cout << getAccess() << endl;
}
for(int i=0; i<3; i++){
acc[i]->displayValues();
}
相关文章:
- 没有为自己的结构调用列表推回方法
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- C++从对象自己的类中删除对象
- 使用 std::optional,而不是自己的结构
- 子轴围绕父轴而不是他自己的轴旋转
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- C++ 如何为自己的迭代器类从迭代器转换为const_iterator?
- 重载 + 自己的类和 std::string 的运算符
- 类无法访问自己的私有静态 constexpr 方法 - Clang bug?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何访问模板参数自己的模板参数?
- 将矩阵乘以我自己的输入的向量
- 您应该在什么时候创建自己的异常类型
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- string1 == string2 和你自己的 for 循环比较有什么区别?
- 如何正确包含我自己的标头?
- 自己的自定义向量类. 内存重新分配
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- C++处理我自己的Exception类