Solved

How to overload << operator for a class?

Posted on 2007-03-23
9
244 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now