473,769 Members | 5,877 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 7212
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
1679
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
2035
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
1918
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
5681
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
1327
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
9423
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
10050
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
9999
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
8876
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
7413
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
6675
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.