C++ 超载的正确方法<;接线员?
我有一个名为user的类,它有一个lname字段。这是过载“的正确方法吗?请尝试:C++ 超载的正确方法<;接线员?,c++,operator-overloading,C++,Operator Overloading,我有一个名为user的类,它有一个lname字段。这是过载“的正确方法吗?请尝试: bool User::operator<(const User& other) const { return lname.compare(other.lname) < 0; } bool User::operator实现operator的正确方法正如其他人所指出的,您的operator我认为最好将“lname”字段隐藏为private return lname.compare(oth
bool User::operator<(const User& other) const
{
return lname.compare(other.lname) < 0;
}
bool User::operator实现operator的正确方法正如其他人所指出的,您的operator我认为最好将“lname”字段隐藏为private
return lname.compare(other.getName()) < 0;
返回lname.compare(other.getName())<0;
第一个返回false;
有点搞砸了。它还可能与const
有关。lname
是什么类型的?你不能只返回lname.compare(other.lname)吗<0
或类似的东西?既然你没有指定比较的内容,很难判断你做的是否正确。Bagh,对不起,返回false不是故意的。为什么对操作符使用==0
或者为什么你认为他的lname不是私有的?这个答案毫无意义,用户::操作符
bool User::operator<( const User& other ) const
bool User::operator<(const User& other) const
class User {
public:
friend bool operator<(const User& u1, const User& u2);
// ...
};
bool operator<(const User& u1, const User& u2)
{
// ...
}
return lname.compare(other.getName()) < 0;