覆盖运算符<<在 C++ 中

Overriding of operator<< in c++

本文关键字:lt C++ 运算符 覆盖      更新时间:2023-10-16

我正在为我的学校做一个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为参数的自由函数。