<< friend

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    << 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::ostringstr eam &operator<< (std::ostringst ream &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::ostringstr eam 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)cybers pace.org | don't, I need to know. Flames welcome.
  • John Carson

    #2
    Re: &lt;&lt; friend

    "Christophe r Benson-Manica" <ataru@nospam.c yberspace.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::ostringstr eam &operator<< (std::ostringst ream &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::ostringstr eam &operator<< (std::ostringst ream &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::ostringstr eam 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)


    Comment

    • Christopher Benson-Manica

      #3
      Re: &lt;&lt; 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::ostringstr eam &operator<< (std::ostringst ream &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)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • John Harrison

        #4
        Re: &lt;&lt; friend


        "Christophe r Benson-Manica" <ataru@nospam.c yberspace.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::ostringstr eam &operator<< (std::ostringst ream &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


        Comment

        • John Harrison

          #5
          Re: &lt;&lt; friend


          "John Harrison" <john_andronicu s@hotmail.com> wrote in message
          news:c1d7dg$1gn 3q7$1@ID-196037.news.uni-berlin.de...[color=blue]
          >
          > "Christophe r Benson-Manica" <ataru@nospam.c yberspace.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::ostringstr eam &operator<< (std::ostringst ream &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::ostringstr eam&.

          Which brings up the real problem with your code, why

          friend std::ostringstr eam &operator<< (std::ostringst ream &ss, TLSFileStream
          &lsf)?

          Why not

          friend std::ostream &operator<< (std::ostream &ss, TLSFileStream &lsf)?

          There are no sensible reasons for the former.

          john


          Comment

          • Christopher Benson-Manica

            #6
            Re: &lt;&lt; friend

            John Harrison <john_andronicu s@hotmail.com> spoke thus:
            [color=blue]
            > Which brings up the real problem with your code, why[/color]
            [color=blue]
            > friend std::ostringstr eam &operator<< (std::ostringst ream &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)cybers pace.org | don't, I need to know. Flames welcome.

            Comment

            Working...