C++ 运算符重载(C+;+;)

C++ 运算符重载(C+;+;),c++,operator-overloading,C++,Operator Overloading,是否可以在另一个类函数而不是主函数中使用重载运算符 示例我在public下有两个类函数: bool Angle::operator< (Angle& a2){...} Angle Angle::operator- (Angle a2){...} bool角度::运算符=a2) {...} 其他的 cout您重载了运算符=。因此,您需要另一个重载函数或更改以前的重载函数: Angle Angle::operator- (Angle a2) { if (*this<a2) co

是否可以在另一个类函数而不是主函数中使用重载运算符

示例我在public下有两个类函数:

bool Angle::operator< (Angle& a2){...}
Angle Angle::operator- (Angle a2){...}
bool角度::运算符<(角度和a2){…}
角度::运算符-(角度a2){…}
我想在第二个函数中使用第一个函数中的重载运算符。我希望第二个函数中的代码如下:

Angle Angle::operator- (Angle a2)
{
if (*this>=a2)
{...}
else
cout<<"You can't subtract greater angle from a smaller one"<<endl;
}
Angle Angle::operator- (Angle a2)
{
    if (!((*this) < a2))
        {...}
    else
        cout<<"You can't subtract greater angle from a smaller one"<<endl;
}
角度::运算符-(角度a2)
{
如果(*此>=a2)
{...}
其他的

cout您重载了运算符
=
。因此,您需要另一个重载函数或更改以前的重载函数:

Angle Angle::operator- (Angle a2)
{
if (*this<a2)
cout<<"You can't subtract greater angle from a smaller one"<<endl;
else
{...}
}
角度::运算符-(角度a2)
{

如果(*this你可以这样写:

Angle Angle::operator- (Angle a2)
{
if (*this>=a2)
{...}
else
cout<<"You can't subtract greater angle from a smaller one"<<endl;
}
Angle Angle::operator- (Angle a2)
{
    if (!((*this) < a2))
        {...}
    else
        cout<<"You can't subtract greater angle from a smaller one"<<endl;
}
角度::运算符-(角度a2)
{
如果(!((*this可以通过重载
运算符>=
?或者通过切换代码来使用
=
?为什么要按引用传递参数?如果不需要更改,则按常量引用或按值传递。