先看下代码:
class Employee
{
public:
Employee(std::string s):name(s),id(count)
{
count++;
}
Employee(std::istream &f):id(count)
{
f>>name;
count++;
}
Employee():name("unknow"),id(count)
{
count++;
}
Employee(const Employee &org):name(org.name),id(org.id)
{
count++;
}
void Display()
{
std::cout<<"-----------------------------"<<std::endl;
std::cout<<"Employee's ID: "<<id<<std::endl;
std::cout<<"Employee's NAME: "<<name<<std::endl;
}
Employee& Employee::operator=(const Employee& other)
{
name=other.name;
id=other.id+3;
return *this;
}
private:
std::string name;
int id;
static int count;
};
int Employee::count=1;
int main()
{
Employee bb("bb");
Employee aa=bb;
aa.Display();
}
我发现好像在执行aa==bb这句时,并没有执行我在类中重载的=操作符,因为id并没有变成4,而是1。大家帮看看是什么问题。非常感谢!