473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 7054
On Sat, 10 Apr 2004 21:38:19 -0400 in comp.lang.c++, "Victor"
<la*********@yahoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostream &) 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*********@yahoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostream &) 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*********@yahoo.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<<(ostream &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*********@yahoo.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<<(ostream &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.west.earthlink.net...
On Sat, 10 Apr 2004 21:38:19 -0400 in comp.lang.c++, "Victor"
<la*********@yahoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostream &) 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.west.earthlink.net...
On Sat, 10 Apr 2004 21:38:19 -0400 in comp.lang.c++, "Victor"
<la*********@yahoo.com> wrote,
Anyone knows how to write a virtual function for operator<< ?


Public:
virtual ostream & print_on(ostream &) 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
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...
5
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...
6
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...
3
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
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
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...
5
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> ...
0
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...
4
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.