本文介绍了运营商超载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在其他类函数而不是主函数中使用重载运算符?
即我在公开场合有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.
推荐答案
Angle Angle::operator- (Angle a2)
{
if (*this<a2)
cout<<"You can''t subtract greater angle from a smaller one"<<endl;
else
{...}
}
这篇关于运营商超载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!