Overload operator << to print as a member

Is there a way to overload the <operator, as a member of a class, to print the values ​​as a text stream. For instance:

 class TestClass { public: ostream& operator<<(ostream& os) { return os << "I'm in the class, msg=" << msg << endl; } private: string msg; }; int main(int argc, char** argv) { TestClass obj = TestClass(); cout << obj; return 0; } 

The only way I could think of is to overload the statement outside the class:

 ostream& operator<<(ostream& os, TestClass& obj) { return os << "I'm outside of the class and can't access msg" << endl; } 

But then the only way to access the private parts of the property would be through friendship with an operator function, and I would rather avoid friends if possible, and thus ask you for alternative solutions.

Any comments or recommendations on how to proceed would be helpful :)

+4
source share
6 answers

You have come across a canonical way of implementing this feature. What you have is right.

+6
source

It must be non-member, because the class forms the second argument of the operator, and not the first. If the output can be performed using only the open interface, then everything is ready. If he needs access to non-public members, you will have to declare him a friend; What are friends for?

 class TestClass { public: friend ostream& operator<<(ostream& os, TestClass const & tc) { return os << "I'm a friend of the class, msg=" << tc.msg << endl; } private: string msg; }; 
+6
source

I believe that one of the popular ways to do this is not a member, not a friendly operator<< , which calls the open non-virtual print method in your class. This printing method can either do the job or delegate it to a secure virtual implementation.

 class TestClass { public: ostream& print(ostream& os) const { return os << "I'm in the class, msg=" << msg << endl; } private: string msg; }; ostream& operator<<(ostream& os, TestClass& obj) { return obj.print(os); } int main(int argc, char** argv) { TestClass obj; cout << obj; return 0; } 
+4
source

You can make it a member of the class that is to the left of << , which is ostream in your case.

However, you can have a base class with a member void do_stream(ostream& o); for all your threads and non-members operator<< that could call it.

+1
source

You are right, this is the only way to implement the stream operator - outside the class.

You need to declare the method as friend .

How is this done.

0
source

You must make it not a member (since the first parameter is not your class).

But you can write it inside your class definition (as a friend):

 class TestClass { public: // Have a nice friend. // This tightly binds this operator to the class. // But that is not a problem as in reality it is already tightly bound. friend ostream& operator<<(ostream& os, TestClass const& data) { return os << "I'm in the class, msg=" << data.msg << endl; } private: string msg; }; 

I see nothing wrong with making this friend.

0
source

Source: https://habr.com/ru/post/1385904/


All Articles