【发布时间】:2018-10-10 05:19:51
【问题描述】:
通过继承,我试图在我的基类中创建一个重载的输出运算符,该运算符调用正确派生类中的输出函数,以根据对象类型打印正确的数据。
如何返回一个可以发送到重载输出运算符的输出,以便仅使用cout << object << endl; 输出对象
例如:
ostream & operator<< (ostream &out, const person &rhs){
out << rhs.print_data();
return out;
}
ostream student::print_data(){
ostream out;
out << data;
return out;
}
ostream faculty::print_data(){
ostream out;
out << data;
return out;
}
编辑:我想弄清楚是否需要在打印函数中返回类型 ostream 或 ostream &
【问题讨论】:
-
是的,谢谢@UlrichEckhardt
标签: c++ inheritance operators ostream