473,394 Members | 1,781 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,394 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 7078
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.