运营商超载问题 [英] Overloading Operators problem
本文介绍了运营商超载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在其他类函数而不是主函数中使用重载运算符?
即我在公开场合有2个班级职能:
Is it possible to use overloaded operator in another class function instead of the main function?
i.e. I have 2 class functions under public:
bool Angle::operator< (Angle& a2){...}
Angle Angle::operator- (Angle a2){...}
我想在第二个函数中使用第一个函数的重载运算符.我希望第二个函数中的代码如下所示:
I want to use the overloaded operator from the first function in the second one. I want the code in the 2nd function to be something like this:
Angle Angle::operator- (Angle a2)
{
if (*this>=a2)
{...}
else
cout<<"You can''t subtract greater angle from a smaller one"<<endl;
}
那么,我可以这样做吗?如果可以的话?
预先感谢.
So, can I do that? And if I can how?
Thanks in advance.
推荐答案
我发现了错误,这是一个愚蠢的错误. :)
我使运算符<我在代码中使用了> =.因此,我需要另一个重载函数或更改先前的重载函数:
I found the error, a silly mistake. :)
I overloaded the operator < and I used >= in the code. So I needed another overloading function or altering the previous one:
Angle Angle::operator- (Angle a2)
{
if (*this<a2)
cout<<"You can''t subtract greater angle from a smaller one"<<endl;
else
{...}
}
这篇关于运营商超载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文