Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual << operator?

Reply
Thread Tools

Virtual << operator?

 
 
Rob McDonald
Guest
Posts: n/a
 
      06-14-2008
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:stream& operator<<(std: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:stream& operator<<(std: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:stream& operator<<(std:stream& s, Base& b) = 0;

Any suggestions are appreciated.

Rob
 
Reply With Quote
 
 
 
 
Frank Birbacher
Guest
Posts: n/a
 
      06-14-2008
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:stream&) const =0;
};

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

Regards,
Frank
 
Reply With Quote
 
 
 
 
Rob McDonald
Guest
Posts: n/a
 
      06-14-2008
On Jun 14, 3:08 pm, Frank Birbacher <(E-Mail Removed)> 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
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      06-15-2008
On Jun 14, 6:25*pm, Rob McDonald <(E-Mail Removed)> wrote:
> On Jun 14, 3:08 pm, Frank Birbacher <(E-Mail Removed)> 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.
 
Reply With Quote
 
Rob McDonald
Guest
Posts: n/a
 
      06-15-2008
On Jun 14, 5:49 pm, "(E-Mail Removed)" <(E-Mail Removed)>
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
 
Reply With Quote
 
David Côme
Guest
Posts: n/a
 
      06-15-2008
On Sat, 14 Jun 2008 22:48:17 +0200, Rob McDonald
<(E-Mail Removed)> 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:stream& operator<<(std: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:stream& operator<<(std: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:stream& operator<<(std: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.
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      06-15-2008
On Jun 14, 9:33*pm, Rob McDonald <(E-Mail Removed)> wrote:
> On Jun 14, 5:49 pm, "(E-Mail Removed)" <(E-Mail Removed)>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
V1.1 Virtual Folder when V2.0 installed for the virtual server? Jj ASP .Net 2 11-30-2005 05:44 PM
virtual template and virtual access for ADSL circuits Gary Cisco 1 04-28-2005 07:26 PM
Virtual Computer Corporation (VCC) Virtual Workbench VW300 Derek Simmons VHDL 0 08-01-2004 04:55 AM
Deploying Servlets in virtual host / virtual filesystem environment while_1 Java 2 06-25-2004 06:00 PM
New Virtual Photo Webring - Register Your Virtual Tour Website! Chuck Arthurs Digital Photography 0 01-26-2004 02:18 PM



Advertisments