Virtual << operator?

Discussion in 'C++' started by Rob McDonald, Jun 14, 2008.

  1. Rob McDonald

    Rob McDonald Guest

    I would like to force all the classes in my hierarchy to implement the
    << operator for testing purposes. My base class is a pure virtual
    class.

    I started out by working with operator overloading in the derived
    class. I have been trying to use what I learned there to create an
    appropriate virtual class to force overloading.


    class Base{

    // Can't define operator with two arguments inside Base class
    virtual std::eek:stream& operator<<(std::eek:stream& s, Base& b) = 0;

    // When I did this for the concrete derived class, it failed because
    the
    // compiler doesn't seem to 'find' the implementation of << for Base
    virtual std::eek:stream& operator<<(std::eek:stream& s) = 0;
    }

    // For the concrete derived class, I got this two-argument approach to
    work.
    // However, you can't declare virtual functions outside a class.
    virtual std::eek:stream& operator<<(std::eek:stream& s, Base& b) = 0;

    Any suggestions are appreciated.

    Rob
     
    Rob McDonald, Jun 14, 2008
    #1
    1. Advertising

  2. Hi!

    Why not use an oridnary virtual function instead of an operator for
    printing?

    Rob McDonald schrieb:
    > Any suggestions are appreciated.


    struct Printable
    {
    virtual void print(std::eek:stream&) const =0;
    };

    static inline std::eek:stream& operator << (
    std::eek:stream& stream, Printable const& p
    )
    {
    if(stream)
    p.print(stream);
    return stream;
    }

    Regards,
    Frank
     
    Frank Birbacher, Jun 14, 2008
    #2
    1. Advertising

  3. Rob McDonald

    Rob McDonald Guest

    On Jun 14, 3:08 pm, Frank Birbacher <> wrote:
    > Hi!
    >
    > Why not use an oridnary virtual function instead of an operator for
    > printing?
    >


    Probably because that is far too simple and logical.

    Worked great.

    Thanks,

    Rob
     
    Rob McDonald, Jun 14, 2008
    #3
  4. Rob McDonald

    Guest

    On Jun 14, 6:25 pm, Rob McDonald <> wrote:
    > On Jun 14, 3:08 pm, Frank Birbacher <> wrote:
    >
    > > Hi!

    >
    > > Why not use an oridnary virtual function instead of an operator for
    > > printing?

    >
    > Probably because that is far too simple and logical.
    >
    > Worked great.
    >
    > Thanks,
    >
    > Rob


    BTW, this exact scenario is covered in The C++ Programming Language.
     
    , Jun 15, 2008
    #4
  5. Rob McDonald

    Rob McDonald Guest

    On Jun 14, 5:49 pm, "" <>
    wrote:
    >
    > BTW, this exact scenario is covered in The C++ Programming Language.


    I didn't see it there when I was trying to figure this out. Is it in
    the operator overloading section, or the streams section, or someplace
    else.

    Thanks,

    Rob
     
    Rob McDonald, Jun 15, 2008
    #5
  6. On Sat, 14 Jun 2008 22:48:17 +0200, Rob McDonald
    <> wrote:

    > I would like to force all the classes in my hierarchy to implement the
    > << operator for testing purposes. My base class is a pure virtual
    > class.
    >
    > I started out by working with operator overloading in the derived
    > class. I have been trying to use what I learned there to create an
    > appropriate virtual class to force overloading.
    >
    >
    > class Base{
    >
    > // Can't define operator with two arguments inside Base class
    > virtual std::eek:stream& operator<<(std::eek:stream& s, Base& b) = 0;
    >
    > // When I did this for the concrete derived class, it failed because
    > the
    > // compiler doesn't seem to 'find' the implementation of << for Base
    > virtual std::eek:stream& operator<<(std::eek:stream& s) = 0;
    > }
    >
    > // For the concrete derived class, I got this two-argument approach to
    > work.
    > // However, you can't declare virtual functions outside a class.
    > virtual std::eek:stream& operator<<(std::eek:stream& s, Base& b) = 0;
    >
    > Any suggestions are appreciated.
    >
    > Rob



    To be virtual a function must be membre of a classe. Or any function
    member receive
    implicitly this* for first argument. To operator<<, the fisrt argument
    must be an stream.
    So you can't have operator<< like a function member, specially virtual.
     
    David Côme, Jun 15, 2008
    #6
  7. Rob McDonald

    Guest

    On Jun 14, 9:33 pm, Rob McDonald <> wrote:
    > On Jun 14, 5:49 pm, "" <>
    > wrote:
    >
    >
    >
    > > BTW, this exact scenario is covered in The C++ Programming Language.

    >
    > I didn't see it there when I was trying to figure this out.  Is it in
    > the operator overloading section, or the streams section, or someplace
    > else.
    >


    In section 21.2.3.1 Virtual Output Functions of C++PL Special Edition.
    It has a non-member operator<< function which takes as the second
    argument
    a base class, but the function just calls a virtual function to do
    it's
    work.

    HTH
     
    , Jun 15, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek Simmons
    Replies:
    0
    Views:
    811
    Derek Simmons
    Aug 1, 2004
  2. Jéjé
    Replies:
    2
    Views:
    407
    Jéjé
    Nov 30, 2005
  3. while_1
    Replies:
    2
    Views:
    638
    Roedy Green
    Jun 25, 2004
  4. Jim Fischer
    Replies:
    3
    Views:
    447
    Jim Fischer
    Jul 31, 2003
  5. Michael Winter
    Replies:
    9
    Views:
    634
    Michael Winter
    Sep 24, 2003
Loading...

Share This Page