Solved

How to overload << operator for a class?

Posted on 2007-03-23
9
247 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

792 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