473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to make operator << a virtual function?

Anyone knows how to write a virtual function for operator<< ?
I have a base class and some public derived class from it.
For the derived class, I hope they can use << to output their
different data. What's more, I want the base class pointers
can access the derived ones and then their corresponding
operator <<. For example:

class base {
// virtual and friend can't be put together, so the following
// code won't work.
virtual friend ostream& operator<< (ostream& os, const base& b);
};
class derived1 : public base
{
virtual friend ostream& operator<< (ostream& os, const base& d1);
};
class derived2 : public base
{
virtual friend ostream& operator<< (ostream& os, const base& d2);
};

I hope they can work like:
base *pb;
derived1 d1;
derived2 d2;
cout<<d1<<"\t"< <d2<<endl; // use their corresponding defination
pb = &d1; // point to class derived1
cout<<*pb<<endl ; // use the operator<< defined in class derived1
pb = &d2; // point to class derived2
cout<<*pb<<endl ; // use the operator<< defined in class derived2

How could I implement the operator overloading then? thanks.
Jul 22 '05 #1
6 7208
On Sat, 10 Apr 2004 21:38:19 -0400 in comp.lang.c++, "Victor"
<la*********@ya hoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostrea m &) const;
} // end class
inline ostream& operator<< (ostream& os, const base& me)
{
return me.print_on(os) ;
}

Jul 22 '05 #2
On Sat, 10 Apr 2004 21:38:19 -0400 in comp.lang.c++, "Victor"
<la*********@ya hoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostrea m &) const;
} // end class
inline ostream& operator<< (ostream& os, const base& me)
{
return me.print_on(os) ;
}

Jul 22 '05 #3
On Sat, 10 Apr 2004 21:38:19 -0400, "Victor" <la*********@ya hoo.com> wrote:
Anyone knows how to write a virtual function for operator<< ?


The canonical way to accomplish this is to have a virtual member function
in each class of your heirarchy, say

class base {
public:
virtual ostream &put(ostream &);
// ...
};

class derived : public base {
public:
ostream &put(ostream &);
// ...
};
// etc.

Each implementation of put() simply "puts" *this to the stream, and returns
the stream reference it was passed. Then you'd write a single non-member
(of course) operator<< as follows:

ostream &operator<<(ost ream &os, const base &b)
{
return b.put(os);
}

This will dispatch to the appropriate virtual put() function when called
with any ref to the base or derived class(es) as the 2nd operand.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
On Sat, 10 Apr 2004 21:38:19 -0400, "Victor" <la*********@ya hoo.com> wrote:
Anyone knows how to write a virtual function for operator<< ?


The canonical way to accomplish this is to have a virtual member function
in each class of your heirarchy, say

class base {
public:
virtual ostream &put(ostream &);
// ...
};

class derived : public base {
public:
ostream &put(ostream &);
// ...
};
// etc.

Each implementation of put() simply "puts" *this to the stream, and returns
the stream reference it was passed. Then you'd write a single non-member
(of course) operator<< as follows:

ostream &operator<<(ost ream &os, const base &b)
{
return b.put(os);
}

This will dispatch to the appropriate virtual put() function when called
with any ref to the base or derived class(es) as the 2nd operand.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5
Good idea. It works. Thanks very much

"David Harmon" <so****@netcom. com> wrote in message
news:40******** ******@news.wes t.earthlink.net ...
On Sat, 10 Apr 2004 21:38:19 -0400 in comp.lang.c++, "Victor"
<la*********@ya hoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostrea m &) const;
} // end class
inline ostream& operator<< (ostream& os, const base& me)
{
return me.print_on(os) ;
}

Jul 22 '05 #6
Good idea. It works. Thanks very much

"David Harmon" <so****@netcom. com> wrote in message
news:40******** ******@news.wes t.earthlink.net ...
On Sat, 10 Apr 2004 21:38:19 -0400 in comp.lang.c++, "Victor"
<la*********@ya hoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostrea m &) const;
} // end class
inline ostream& operator<< (ostream& os, const base& me)
{
return me.print_on(os) ;
}

Jul 22 '05 #7

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

Similar topics

3
8266
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it at all possible?
5
1677
by: Yang Lee | last post by:
Hi, Could you please try to run the following program it doesnt work for operator overloading function. It doesn't print peoper empName for emp2 object. It prints junk. Just what could be the cause. I get values for emp2 object from following DeltaEmp emp2=*emp1+10; where emp1 is a pointer to object of class DeltaEmp.
6
320
by: Victor | last post by:
Anyone knows how to write a virtual function for operator<< ? I have a base class and some public derived class from it. For the derived class, I hope they can use << to output their different data. What's more, I want the base class pointers can access the derived ones and then their corresponding operator <<. For example: class base { // virtual and friend can't be put together, so the following // code won't work.
3
2034
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
3
1917
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
2
5680
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
5
5699
by: Karl | last post by:
Hey everyone! So I'm writing my own String class to wrap std::string and implement an API as close to identical as possible to the Java API. It's pretty small so far: #include <string> class String {
0
1326
by: Adrian | last post by:
Hi All, I am trying to create an output operator that doesnt rely on the stream type and is also polymorphic. Is there a way to combine these 2 methods so that the polymorphic output operator has template arguments. I have tried a few ways and my compiler just wont let me. Method 1: Doesn't rely on the char type of a stream --------------------------------------------------- class A
4
3379
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
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();...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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

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.