Overloading operations for classes with names

So let's say I have the following class:

namespace Example { class Bar {}; } 

Now, if I want to overload the operators for the Bar class, should I do:

  namespace Example { class Bar {}; ostream& operator<<(ostream& os, const Bar& b) {/*..........*/} } 

or should I do:

 namespace Example { class Bar {}; } ostream& operator<<(ostream& os, const Example::Bar& b) {/*..........*/} 

If I have to do one of the above, please write an explanation of why this should be done this way.

PS /*.....*/ just means the body of functions (omitted for simplicity)

+4
source share
1 answer
  #include<iostream> using namespace std; namespace Example { class Bar {//........ friend ostream& operator<<(ostream& os, const Bar& b); }; } ostream& operator<<(ostream& os, const Example::Bar& b) {//.......... } int main() { Example :: Bar b; // out<<b; I am not sure About this return 0; } 

Hope I did well this time

-2
source

All Articles