I give the following example to illustrate my question:
In the first example, operator overloading is insider the class definition:
// Example program 1
#include <iostream>
#include <string>
class Abc
{
public:
int a_;
Abc(int a):a_(a) {};
bool operator <(const Abc &obj)
{
return this->a_<obj.a_;
}
};
int main()
{
Abc myAbc(3);
Abc yourAbc(4);
bool b = (myAbc<yourAbc);
if(b)
std::cout<<"True";
else
std::cout<<"False";
return 0;
}
In the second example, operator overloading is outsider the class definition:
// Example program 2
#include <iostream>
#include <string>
class Abc
{
public:
int a_;
Abc(int a):a_(a) {};
};
bool operator <(const Abc &objLeft,const Abc &objRight)
{
return objLeft.a_<objRight.a_;
}
int main()
{
Abc myAbc(3);
Abc yourAbc(4);
bool b = (myAbc<yourAbc);
if(b)
std::cout<<"True";
else
std::cout<<"False";
return 0;
}
In both examples, the codes work well. So my question is which one is better? Or they are the same, and it is only a matter of style. Thanks.