473,761 Members | 7,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual << operator?

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::ostream& operator<<(std: :ostream& 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::ostream& operator<<(std: :ostream& 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::ostream& operator<<(std: :ostream& s, Base& b) = 0;

Any suggestions are appreciated.

Rob
Jun 27 '08 #1
6 4734
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::ostr eam&) const =0;
};

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

Regards,
Frank
Jun 27 '08 #2
On Jun 14, 3:08 pm, Frank Birbacher <bloodymir.c... @gmx.netwrote:
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
Jun 27 '08 #3
On Jun 14, 6:25*pm, Rob McDonald <rob.a.mcdon... @gmail.comwrote :
On Jun 14, 3:08 pm, Frank Birbacher <bloodymir.c... @gmx.netwrote:
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 27 '08 #4
On Jun 14, 5:49 pm, "AnonMail2...@g mail.com" <AnonMail2...@g mail.com>
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
Jun 27 '08 #5
On Sat, 14 Jun 2008 22:48:17 +0200, Rob McDonald
<ro************ @gmail.comwrote :
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::ostream& operator<<(std: :ostream& 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::ostream& operator<<(std: :ostream& 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::ostream& operator<<(std: :ostream& 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.
Jun 27 '08 #6
On Jun 14, 9:33*pm, Rob McDonald <rob.a.mcdon... @gmail.comwrote :
On Jun 14, 5:49 pm, "AnonMail2...@g mail.com" <AnonMail2...@g mail.com>
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 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
17771
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
8
2539
by: sun1991 | last post by:
Hi All, I tried the following code, but it did not work as I think: --- using namespace std; namespace { class Fraction { public:
14
2343
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a hash string class ) are derived. The two derived classes are template classes specifying the sizes. The base class is a non-template class so that it can be used generically in the interface classes. the design would look like
1
6712
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.