How does the global operator work with class data? What are his qualities, and how does he differ from friend?

class Demo { private: double d; public: Demo() { d = 0.0; } Demo(double x) { d = x; } ~Demo() { cout <<"In Destructor" << endl; } double getd() { d = 5; return d; } }; bool operator<(Demo a, Demo b) { return a.getd() < b.getd(); } bool operator==(Demo a, Demo b) { return a.getd() == b.getd(); } int main() { Demo o1(2.0),o2(3.0); if(o1<o2) cout << "True"; cout << endl; return 0; } 

Closed due to the fact that the question’s essence is not clear to the participants by Vlad from Moscow , user194374, Harry , αλεχολυτ , fori1ton Jan 20 '17 at 18:29 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Arshakyan The question is not clear. - Vlad from Moscow

1 answer 1

How does the global operator work with class data?

As you wrote:

 return a.getd() == b.getd(); 

(BTW, for double this comparison method is incorrect).

what is different from friend.

Lack of access to private and protected.