[英]Overloaded operator for C++ class doesn't get called
In my class, I write the prototype (public) as: 在我的班上,我把原型(公共)写成:
bool operator< (const MyClass& obj);
I implement the method outside the class (in the same file) as: 我在类之外(在同一个文件中)实现方法:
bool MyClass::operator< (const MyClass& obj)
{
cout << "operator< used" << endl;
//do my work
}
The problem is that although my overloading operator gets called if I call it explicitly (like obj1->operator<(*obj2)
) but not when called implicitly (like obj1 < obj2
). 问题是,尽管我的重载运算符被显式调用(如
obj1->operator<(*obj2)
)而被隐式调用(如obj1 < obj2
)却未被调用。
I have followed overloading tutorial from this article and I can't see what I'm missing or doing wrong. 我已经按照本文中的重载教程进行了操作,但看不到缺少或做错的事情。
obj1
和obj2
是指针,因此您可以执行*obj1 < *obj2
。
obj1 and obj2 are pointers to MyClass. obj1和obj2是指向MyClass的指针。 if you want to call operator < use: *obj1 < *obj2
如果你想调用operator <use:* obj1 <* obj2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.