覆盖运算符<<在 C++ 中
Overriding of operator<< in c++
我正在为我的学校做一个C++项目
我有两个班:就业和教师。Teacher派生自Employee,并具有其函数的覆盖。
我们覆盖运算符<<
来打印Employees或Teachers的一些信息。每个类都有一个const int attribute LevelAcces_
。对于员工,是5;对于教师是20。
当我在main.cpp中创建教师时,我调用运算符<lt;让老师打印他的信息。所以这个函数被称为:
ostream& operator<<(ostream& os, const Teacher& pTeacher){
os << (pTeacher);
return os;
}
但是,函数用行"os << (pTeacher);"
调用自己,并且执行一个导致堆栈溢出的循环。
我希望线路"os << (pTeacher)"
呼叫运营商<lt;是我班的老板,而不是我班的老师
运算符的覆盖<lt;在职:
ostream& operator<<(ostream& os, const Employe& pEmploye){
os << "Name: " << pEmploye.name_ << endl;
os << "Class: " << pEmploye.getClass() << endl;
os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;
return os;
}
我试图将我的"老师"转换为"员工",但当它打印消息时,LevelAcces为5(我想要20,因为我的"员工"是一名教师)。
我还尝试使用Employee::operator<lt;但是运算符<lt;不是Employee的成员,所以它不起作用。。。
所以,我的问题是:
如何使用我的运算符<lt;在我的操作员中的Employe<lt;是否打印正确的信息(LevelAccess=20而不是5)
我也在想"虚拟",但我们的教授告诉我们没有必要使用这个词
提前感谢:)
这里有一个更完整的代码:
main.cpp:
Teacher Garry("Garry");
cout << Garry << endl;
Employee.cpp:
#include "Employe.h"
using namespace std;
Employe::Employe(){
name_ = "";
}
Employe::Employe(string pName){
name_ = pName;
}
string Employe::getName() const{
return name_;
}
unsigned int Employe::getLevelAccess() const{
return levelAccess_;
}
string Employe::getClass() const{
return typeid(*this).name();
}
ostream& operator<<(ostream& os, const Employe& pEmploye){
os << "Name: " << pEmploye.name_ << endl;
os << "Class: " << pEmploye.getClass() << endl;
os << "LevelAcces: " << pEmploye.getLevelAccess() << endl;
return os;
}
在Employe.h:中有这个
private:
static const unsigned int LevelAccess_ = 5;
Teacher.cpp:
#include "teacher.h"
using namespace std;
Teacher::Teacher(string pName){
nom_ = pName;
}
unsigned int Teacher::getLevelAccess() const{
return(Employe::getLevelAccess() + accessTeacher_);
}
string Teacher::getClass() const{
return typeid(*this).name();
}
ostream& operator<<(ostream& os, const Teacher& pTeacher){
os << (pTeacher);
return os;
}
这是老师。h:
static const unsigned int accesTeacher_ = 15;
我要做的是:只定义一个
ostream& operator<<(ostream& os, const Employe& pEmploye)
{
return pEmploye.display(os);
}
对于层次结构的基础,在其中调用受保护的成员函数virtual display()
,该函数被每个派生类重写,并且显示被委派到该函数。这有时被称为NVI(非虚拟接口)习惯用法。它是这样工作的:
class Employee
{
// ...
friend ostream& operator<<(ostream& os, const Employee& pEmployee)
{
return pEmployee.display(os);
}
protected:
virtual ostream& display(ostream& os) const
{
// implement it here
return os;
}
};
class Teacher: public Employee
{
// ....
protected:
ostream& display(ostream& os) const override
{
// implement it here
return os;
}
};
您可以使用cast:
os << static_cast<const Employe &>(pTeacher);
&
非常重要。
要从Employe
引用调用成员函数Teacher::getLevelAccess()
,必须使该函数成为虚拟函数。(在teacher.h
中执行此操作)。getClass()
也应该是virtual
。
注:。你一直在说"Override of operator<<
in Employee:"之类的话,但你在Employee中没有重载operator<<
。您有一个以Employe
为参数的自由函数。
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何显式调用运算符<<
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 左角支架解释为操作员&lt;而不是模板参数
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- 超载操作员&lt;&lt; - 必须是二进制操作员
- 没有匹配的“运营商&lt;&lt;”