 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Peter v. N. Guest
|
Posted: Sun Nov 19, 2006 10:11 am Post subject: Overloading <<, number of arguments |
|
|
Hi all,
Maybe this has been asked a million times before.
In that case I'm sorry for being to lazy to search
the Internet or look it up in a decent C++ reference:
I read in O'Reilly's C++ Pocket reference (see also
http://www.oreilly.com/catalog/cpluspluspr/errata/ ,
btw not that bad),
that the operator << has to be overloaded like this:
#include <ostream>
std::ostream &MyClass::operator<<(std::ostream &pOS, MyClass &pObj) const
{
return pOS << pObj.someMethod();
}
Implemented that way, Eclipse 3.2 (CDT) with gcc 4.0.1
(i686-apple-darwin8-gcc-4.0.1) report that << must take one and only
one argument. That's strange, isn't it?
What's the default implementation now?
Thank you for a hint pointing me in the right direction.
Brgds,
Peter |
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Nov 19, 2006 10:11 am Post subject: Re: Overloading <<, number of arguments |
|
|
"Peter v. N." <skull2crush (AT) hotmail (DOT) com> wrote in message
news:4f8a8$455fe49f$50dbfb67$12549 (AT) news (DOT) hispeed.ch...
| Quote: | Hi all,
Maybe this has been asked a million times before.
In that case I'm sorry for being to lazy to search
the Internet or look it up in a decent C++ reference:
I read in O'Reilly's C++ Pocket reference (see also
http://www.oreilly.com/catalog/cpluspluspr/errata/ ,
btw not that bad),
that the operator << has to be overloaded like this:
#include <ostream
std::ostream &MyClass::operator<<(std::ostream &pOS, MyClass &pObj) const
{
return pOS << pObj.someMethod();
}
|
Then it's wrong.
| Quote: |
Implemented that way, Eclipse 3.2 (CDT) with gcc 4.0.1
(i686-apple-darwin8-gcc-4.0.1) report that << must take one and only
one argument. That's strange, isn't it?
|
No.
| Quote: |
What's the default implementation now?
Thank you for a hint pointing me in the right direction.
|
There are two possible ways to write this overload;
as a member function, in which case the signature is:
std::ostream& T::operator<<(std::ostream&);
(an object of the user defined type ('T') is implied
as the left-hand operand.)
or as a non-member function:
std::ostream& operator<<(std::ostream&, const T&);
I suggest using a real C++ text(s) as your primary information
source, rather than a 'pocket guide'.
-Mike |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Sun Nov 19, 2006 10:11 am Post subject: Re: Overloading <<, number of arguments |
|
|
Peter v. N. wrote:
| Quote: | Hi all,
Maybe this has been asked a million times before.
In that case I'm sorry for being to lazy to search
the Internet or look it up in a decent C++ reference:
I read in O'Reilly's C++ Pocket reference (see also
http://www.oreilly.com/catalog/cpluspluspr/errata/ ,
btw not that bad),
that the operator << has to be overloaded like this:
#include <ostream
std::ostream &MyClass::operator<<(std::ostream &pOS, MyClass &pObj) const
{
return pOS << pObj.someMethod();
}
|
No sir, thats impossible. All non-static and non-const member functions
have a hidden this parameter that already passes the object itself. So
the above, as clearly mentioned by the compiler, needs only a reference
to a std::ostream and it can't be const.
Only a dead brain would think that passing an instance of MyClass to
another instance of MyClass to stream its contents makes sense. It
doesn't and it never will.
std::ostream& MyClass::operator<<(std::ostream& os)
{
...
}
And, since such an operator is more usefull as a global operator
overload, its often found as a friend of the class with a const
reference to an instance of the class:
MyClass
{
...
friend std::ostream&
operator<<(std::ostream& r_os, const MyClass& r_obj)
{
... // implemeneted here or globally
}
};
Now please note the placement of the reference operator. Its critical
because it clearly states the intentions of the operator. These are
references, not addresses.
| Quote: |
Implemented that way, Eclipse 3.2 (CDT) with gcc 4.0.1
(i686-apple-darwin8-gcc-4.0.1) report that << must take one and only
one argument. That's strange, isn't it?
What's the default implementation now?
|
Typically, the default is the global op<< declared as a friend. Why?
Because these are often templated and they need to be globally
accessisible when streaming to std::cout.
| Quote: |
Thank you for a hint pointing me in the right direction.
Brgds,
Peter |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|