0

For example I need to overload == operator. How can I do it outside a class . (Without prototype).

aleshka-batman
  • 187
  • 1
  • 13
  • This question is best answered with a good C++ book. Perhaps you should have a look at the [C++ recommended book list](http://stackoverflow.com/questions/388242/). – jaggedSpire Feb 02 '17 at 15:56
  • I've read books, but this seems to be impossible for me. This operator need an acces to private fields. But this cant be done outside a class. – aleshka-batman Feb 02 '17 at 15:58
  • 1
    @aleshka-batman it can, with the adequate [`friend` declaration](http://en.cppreference.com/w/cpp/language/friend). – Quentin Feb 02 '17 at 16:00
  • @Quentin without friend keyword. – aleshka-batman Feb 02 '17 at 16:02
  • @aleshka-batman You can't. If you need access to the private data then it needs to be a member function or a friend function. – NathanOliver Feb 02 '17 at 16:03
  • Only a class and its friends can access its private members. So, no, you can't access private members without the means to access private members. Why do you need to perform a task while banning the required tools? – Quentin Feb 02 '17 at 16:05
  • @NathanOliver i've found a solution using the link you gave me. class X { X& operator+=(const X& rhs) { // actual addition of rhs to *this return *this; } }; inline X operator+(X lhs, const X& rhs) { lhs += rhs; return lhs; } operator(+) is defined outside the class,but it calls overloaded operator (+=)inside the class – aleshka-batman Feb 02 '17 at 16:07
  • 1
    @aleshka-batman That `operator+` relies on `operator+=` being public. – molbdnilo Feb 02 '17 at 16:11

0 Answers0