<< friend | | |
Given
class Fred {
public:
friend std::ostream& operator<< (std::ostream& o, const Fred& fred);
...
private:
int i_; // Just for illustration
};
std::ostream& operator<< (std::ostream& o, const Fred& fred)
{
return o << fred.i_;
}
from the FAQ, what is wrong with the following class declaration?
(How TLSFile is implemented isn't really germane here)
class
TLSFileStream : public TLSFile
{
private:
std::string buf;
public:
friend std::ostringstream &operator<< (std::ostringstream &ss, TLSFileStream &lsf)
{return(ss << lsf.AsString());} // I get a warning about
// references initialized with
// ostream - huh??
WriteStr( std::str s );
};
template <class T>
TLSFileStream& operator<< (TLSFileStream& lsf, const T& t)
{
std::ostringstream ss;
ss << t; // trying to use << operator above, says that << isn't
// implemented for TLSFile - huh?? ;(
lsf.WriteStr( ss.str() );
return lsf;
};
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. | | | | re: << friend
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in
message news:c15rpt$puv$1@chessie.cirr.com[color=blue]
> Given
>
> class Fred {
> public:
> friend std::ostream& operator<< (std::ostream& o, const Fred&
> fred); ...
> private:
> int i_; // Just for illustration
> };
>
> std::ostream& operator<< (std::ostream& o, const Fred& fred)
> {
> return o << fred.i_;
> }
>
> from the FAQ, what is wrong with the following class declaration?
> (How TLSFile is implemented isn't really germane here)
>
> class
> TLSFileStream : public TLSFile
> {
> private:
> std::string buf;
>
> public:
> friend std::ostringstream &operator<< (std::ostringstream &ss,
> TLSFileStream &lsf) {return(ss << lsf.AsString());} // I get a
> warning about // references
> initialized with // ostream -
> huh?? WriteStr( std::str s );
> };[/color]
Try
friend std::ostringstream &operator<< (std::ostringstream &ss,
TLSFileStream &lsf)
{
ss << lsf.AsString();
return ss;
}
I presume this is a typo (cut and paste is always better than typing), but
WriteStr( std::str s );
is missing a return type and there is no such thing as std::str.
[color=blue]
> template <class T>
> TLSFileStream& operator<< (TLSFileStream& lsf, const T& t)
> {
> std::ostringstream ss;
> ss << t; // trying to use << operator above, says that << isn't
> // implemented for TLSFile - huh?? ;(
> lsf.WriteStr( ss.str() );
> return lsf;
> };[/color]
Junk the concluding semi-colon.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) | | | | re: << friend
John Carson <donaldquixote@datafast.net.au> spoke thus:
[color=blue][color=green]
>> TLSFileStream &lsf) {return(ss << lsf.AsString());} // I get a
>> warning about // references
>> initialized with // ostream -[/color][/color]
[color=blue]
> friend std::ostringstream &operator<< (std::ostringstream &ss,
> TLSFileStream &lsf)
> {
> ss << lsf.AsString();
> return ss;
> }[/color]
This worked, but can you explain why this is correct while my version
was wrong?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. | | | | re: << friend
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c1d5o8$cg0$1@chessie.cirr.com...[color=blue]
> John Carson <donaldquixote@datafast.net.au> spoke thus:
>[color=green][color=darkred]
> >> TLSFileStream &lsf) {return(ss << lsf.AsString());} // I get a
> >> warning about // references
> >> initialized with // ostream -[/color][/color]
>[color=green]
> > friend std::ostringstream &operator<< (std::ostringstream &ss,
> > TLSFileStream &lsf)
> > {
> > ss << lsf.AsString();
> > return ss;
> > }[/color]
>
> This worked, but can you explain why this is correct while my version
> was wrong?
>[/color]
Because the result of ss << lsfAsString() is a temporary (like the result of
all function calls).
You cannot bind non-const references to a temporary.
john | | | | re: << friend
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c1d7dg$1gn3q7$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
> news:c1d5o8$cg0$1@chessie.cirr.com...[color=green]
> > John Carson <donaldquixote@datafast.net.au> spoke thus:
> >[color=darkred]
> > >> TLSFileStream &lsf) {return(ss << lsf.AsString());} // I get a
> > >> warning about // references
> > >> initialized with // ostream -[/color]
> >[color=darkred]
> > > friend std::ostringstream &operator<< (std::ostringstream &ss,
> > > TLSFileStream &lsf)
> > > {
> > > ss << lsf.AsString();
> > > return ss;
> > > }[/color]
> >
> > This worked, but can you explain why this is correct while my version
> > was wrong?
> >[/color]
>
> Because the result of ss << lsfAsString() is a temporary (like the result[/color]
of[color=blue]
> all function calls).
>
> You cannot bind non-const references to a temporary.
>[/color]
Actually that might not be right, a definitive answer would need to know the
return type of lsfAsString(), and what overloads were declared on operator
<< for ostringstream and the return type of lsfAsString() and what the
return type of that overload was.
But I'm guessing that operator << as used in 'ss << lsf.AsString()' returns
a std::ostream&, which cannot be converted to a std::ostringstream&.
Which brings up the real problem with your code, why
friend std::ostringstream &operator<< (std::ostringstream &ss, TLSFileStream
&lsf)?
Why not
friend std::ostream &operator<< (std::ostream &ss, TLSFileStream &lsf)?
There are no sensible reasons for the former.
john | | | | re: << friend
John Harrison <john_andronicus@hotmail.com> spoke thus:
[color=blue]
> Which brings up the real problem with your code, why[/color]
[color=blue]
> friend std::ostringstream &operator<< (std::ostringstream &ss, TLSFileStream
> &lsf)?[/color]
[color=blue]
> Why not[/color]
[color=blue]
> friend std::ostream &operator<< (std::ostream &ss, TLSFileStream &lsf)?[/color]
Ooh, good call, although...
[color=blue]
> There are no sensible reasons for the former.[/color]
Does the fact that there is pretty much no way a this class will be
used with any ostream other than an ostringstream count for anything?
I know you're going to say "Well, what about future users?" - let's
just say that when someone wants to use this object with a regular
ostream, the need for the object itself will likely be gone :)
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|