?
Solved

How to overload << operator for a class?

Posted on 2007-03-23
9
Medium Priority
?
253 Views
Last Modified: 2010-04-01
Hi,

How do we overload the stream operator for a class again? I want to do this:

class Something {
    int a;
    double d;
    void OverloadedStreamOperator <<()
    {
        ????
    }
};

int main()
{
    Something s;
    s.a = 44;
    s.d = 34.34;
    cout << s << endl;
}

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18782467
 ostream& operator<< (ostream& os, const Something& s)
  {
    return os << s.a << " " << s.d << endl;
  }
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 18782538
I tried adding that as a member of my class but get this compiler error:

     binary 'operator <<' has too many parameters
0
 
LVL 86

Expert Comment

by:jkr
ID: 18782537
Note that this is not a member operator, but an operator at global scope, so to make that work you'll usually have to make it a 'friend' of your class to use for non-public members also, i.e.

 ostream& operator<< (ostream& os, const Something& s)
  {
    return os << s.a << " " << s.d << endl;
  }
class Something {

friend ostream& operator<< (ostream&, const Something&);

    int a;
    double d;

};
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DJ_AM_Juicebox
ID: 18782552
is there any way to make it a member of the class? Otherwise I'll have all these friends for all my classes at global scope?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18782563
Or completely with your class :

class Something {
    int a;
    double d;
  public :
    friend istream& operator>>(istream& is, Something& s);
    friend ostream& operator<<(ostream& os, const Something& s);
};

istream& operator>>(istream& is, Something& s) {
  is >> s.a;
  is >> s.d;
  return is;
}
ostream& operator<<(ostream& os, const Something& s) {
  os << "a = " << s.a << endl;
  os << "d = " << s.d << endl;
  return os;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 18782586
>>is there any way to make it a member of the class?

No, the '<<' operator has a 'left to right' association. You can make a

Something s;

s << "whatever";

a member, but not a

ostream os;

os << s;
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 18782660
BTW, you could also add a 'write()' method like

class Something {
    int a;
    double d;
    ostream& write(ostream& os) const
    {
        os << a;
        os << d;

        return os;
    }
};

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 18782690
yeah i ended up just doing a write() function. How sad!

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 18782711
I'd actually have chosen the 'friend' approach. It is just a C++ concept that your friends have access to your privates (pardon the pun ;o)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question