Does Overloading Operator<< works inside the class?

17,291

Solution 1

The member function:

ostream &operator<<(ostream &os);

does work, but not for the situation you want. It will be called when you do something like:

A a;
a << std::cout;

i.e. where the object is the left-hand side of the operator.

Solution 2

The problem is that your operator<< would take ostream as a second parameter, not a first one. That way, you could do myObject << std::cout, but it would look unintuitive and you would not be able to chain calls due to operator<< being left-associative.

Another benefit of declaring the operator as a friend as opposed to a member function is that automatic conversions can occur. That means that if you have a class B that doesn't derive from A but does have a B(A const&) constructor, you will still be able to do std::cout << my_b; and have it converted to an A and then printed.

Fortunately, defining operator<< as a friend can be done within the class if you prefer to. Your code could be written as

class A {
    int i;
    friend std::ostream& operator<<(std::ostream& o, A const& a) {
        o << a.i;
        return o;
    }
};

Why doesn't the standard allow for you to specify which side the argument should go on? Let's pretend it did, and add the left_of and right_of keywords to specify:

struct B;

struct A {
     A left_of operator+(B const&) {
         return *this;
     }
};

struct B {
     B right_of operator+(A const&) {
         return *this;
     }
};

What happens now when we do A a; B b; f(a + b);? Each class has an operator that handles this case, which means we can't decide. Seeing as many operators should be friends due to the conversion possibilities anyway, not allowing this kind of thing is not that big a problem, and prevents these kind of ambiguities. (Of course, you could also define a member operator and a free one, which would cause a very similar problem.)

By the way, as things are, the definition of your friend operator<< isn't returning anything, which will mess up chaining.

Solution 3

Remember that for a non static member function of a class first argument is this pointer. So the signature of your the "operator<<" function, after compilation will become:

ostream &operator<<(A *this, ostream &os);

This will work when you do something like:

A obj;
obj << std::cout;

Think of "obj" as first argument and "std::cout" as 2nd argument to the operator "<<".

Better way to overload "<<" is by using friend function because in this way you can make your overloaded operator associative.

Share:
17,291

Related videos on Youtube

howtechstuffworks
Author by

howtechstuffworks

I am Student from an university in North East. I had a bad habit of coding in a language, just by seeing examples and browsing/googling syntax online, thats how I completed most of my assignments, but better late than never, I realized that this is a problem, when you go into research, where you need to think in terms of language and understand the underlying mechanisms to become a better coder, which is what after all, I am striving for. Anyways, I think I will learn more here, than I could ever contribute.

Updated on June 04, 2022

Comments

  • howtechstuffworks
    howtechstuffworks almost 2 years

    I mean, I was trying to overload the operator<< inside the class

    like this

     class A {
          public:
               ostream &operator<<(ostream &os);// which doesnt work
    
          private:
               friend ostream &operator<<(ostream &os, const A& a); //Works
               int i;
    
     };
    
      Definition
              ostream &operator<<(ostream &os, const A& a) {
                  os<<a.i;
                  return os;
              }
    

    why can't I overload the operator inside the class specific to class? or Am I missing something? or Am I stupid to even think in such a way? Please advise.

  • howtechstuffworks
    howtechstuffworks about 12 years
    ^ got u..... But the first one doesn't make sense at all..... was it done, because to maintain consistency among all operators, where all the things appear right to the operator, is taken as a parameter?
  • howtechstuffworks
    howtechstuffworks about 12 years
    ^ thanks anton...... I understood lot better now..... Btw, I agree ur argument for + operator... I am not arguing, that we should have to specify which way it is to be done... I agree that all the operators by default take the right value, but what happens if we decide to do it the other way? default by left, for operators in need? like <<.... << operator always takes the left as argument and bind to the class specified in right..... Or was it overlooked, because of the two disadv, yout mentioned at the top of the comment...????
  • Anton Golov
    Anton Golov about 12 years
    @howtechstuffworks: While using operator<< for output is common, it is not the only use, and making some operators take the value to the right and the others the value to the left would be even more of a headache.