0

I am trying to overload << operator, so that when I use my class object in std::cout, it prints out the data members which is supposed to get printed.

I know I have to define a friend functions signature inside of a class, and then outside of the class I define two friend functions doing the job, but I want to hand over the class, and don't want the user to do literally anything and I want the class take care of everything.

How can I achieve such a thing in C++?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hossein
  • 24,202
  • 35
  • 119
  • 224
  • See this answer about how to define the friend inside the class http://stackoverflow.com/a/2077010/597607 – Bo Persson Apr 17 '12 at 11:09
  • Why would this even matter? I mean, since it doesn't matter (I think), this question is useless (and thus warrants downvotes). If it matters, it helps to know why it matters, because then, instead of trying to solve the problem in an impossible way, one can try to actually solve it some other way. – R. Martinho Fernandes Apr 17 '12 at 11:21
  • @R.MartinhoFernandes I think it's a good question, basically he wants to achieve what ADL does in its unintuitive way. The problem being somewhat obscure, it's hard to formulate well. – Potatoswatter Apr 17 '12 at 11:32
  • I honestly can't read that in this question. If that's the case, I still think the OP needs to make that clear. – R. Martinho Fernandes Apr 17 '12 at 11:38
  • Specifically, I think he's creating a header-only library and wants to eliminate the `.cpp` component which he considers to be "the user." Which could also be achieved by defining the friend as stated but making it `inline`… however my answer is better :v) – Potatoswatter Apr 17 '12 at 11:40
  • @Potatoswatter : yes thats exactly what i was after :) but i am happy i learned about ADL :) – Hossein Apr 18 '12 at 16:30

3 Answers3

2

C++ is designed to allow non-member functions to do things that would require a member function in other languages. A friend function is very similar to a member function, in that it can be defined within a class scope. Also (and this can be confusing) if you declare a friend function only within a class scope, it can only be accessed by passing an object of that class to it.

struct printme {
    friend std::ostream &operator<< ( std::ostream &os, printme const & )
        { return os << "hello"; }

    friend void named_friend( printme const & ) {}
};

std::cout << printme(); // OK
named_friend( printme() ); // OK
void (*fn_ptr) = named_friend; /* function not found
           it can only be found by association with printme */

Although this subtle rule is odd at first, it reflects the idea that nonmember functions can also be encapsulated by the class.

For more info, read up on argument-dependent lookup (ADL).

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
0

we usually uses << in the following way:

cout<<myobject;

if you make operator<<() a member function of your class you have to use the following syntax to call operator<<()

myobject<<cout;

do you want it that way? if its ok for you , you can make it a member function .

if you want it like

cout<<myobject 

- you have to make operator<<() a friend function of your class

whitetiger
  • 412
  • 1
  • 4
  • 13
-1

Here is an example:

#include <iostream>
#include <string>

using namespace std;

class A {
public:
    string pr() { return "Hi, I'm class A"; }
};

ostream &operator << (ostream &o, const A &a) {
    o << a.pr();
    return o;
}

int main() {
    A a;

    cout << a << endl;

    return 0;
}

However, from my experience, don't do that. Oveloading is academically nice, but it makes it diffult to search your application for its implementation, and even comprehend that << has been overloaded. I always use something of the form cout << myobject.tostring()

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
  • thank you very much :) i recently tried to use boost , and in meantime , i saw alot of this overloading , which made my life really easy , specially when using boost::serialize , then i thought to myself , why wouldnt i do this myself and make my like easier ? :) and yes you are right , i have never thought about this issue . I'll have this in mind for sure :) again Thank you sir :) – Hossein Apr 17 '12 at 11:40