Tuesday 15 January 2013

c++ - private friend operator<< -


Then I have a class for which I have an overload operator < & Lt; Output your internal data in the output stream; I want to do this for debugging purposes only and in this way somehow the operator & lt; & Lt; To hide from the outside world, so that it is only the * .cpp file where the implementation of my class remains operator & lt; & Lt; has made me friend to join my class in the member variable. Although in the class the operator & lt; & Lt; Announcing Friends enables anyone from outside the world to call operator on this class ...

I know I want to do this I can work as a regular private member, but I already have some debugging macros which are the operator & lt; & Lt; , so I was wondering if it is possible to do it in some way

You can use the operator in a supporting proxy class & lt; & Lt; can move functionality. When the proxy is & lt; & Lt; Used as RHS of , the original item is printed. Define a personal built-in conversion from the original to the proxy now let anyone operator & lt; & Lt; , but only the class has the ability to create a proxy.

  class private_printable {int state; Structure Proxy {Private_procable bracket & amp; R; }; Operator proxy () const {refund {* this}; } Buddy Stud :: Ostream & amp; Operator & lt; & Lt; (Stud :: Ostream and Proxy Cost & amp; O) {Returns S & LT; & Lt; O.r.state; } Public: Private_Printable (): State (5) {} zero debug () {std :: cout & lt; & Lt; * This is & lt; & Lt; '\ N'; }};   

Note that the proxy is not required to be a friend. The only change in the normal way of working is to have a proxy and a conversion function. Friend Operator & lt; & Lt; Namespace-scope is found by logic-based lookup without declaration, even if it does not take any private_printable argument, then the conversion makes it viable so do not do that cleaner solution Possible: v).

No comments:

Post a Comment