Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to overload << operator for a class?

Posted on 2007-03-23
9
Medium Priority
?
256 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 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