Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

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
{...}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900