Virtual << operator?

R

Rob McDonald

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
 
F

Frank Birbacher

Hi!

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

Rob said:
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
 
R

Rob McDonald

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
 
R

Rob McDonald

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
 
D

David Côme

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.
 
A

AnonMail2005

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top