Polymorphism & Overloaded Operators :: C++
Hi.
I would like to know is it possible to apply polymorphism to overloaded operators? For examples, is it possible to rely on polymorphism via creating virtual overloaded operators and friend functions?
Consider the ostream and istream for instance. I would like to design a hierarchy such as this.
code:-----------------------------------------------------------
Base *pB;
Derived *pD = Derived();
pB = dynamic_cast<base *>(pD);
// I would like this line to call an ostream friend function in the *Derived* class.
cout << *pB;
----------------------------------------------------------------
My code might be off. Nonetheless, the point I want to get at is applying polymorphism to overloaded functions and friend functions, especially iostream.
Thanks,
Kuphryn
Generally yes, but operator<< is a bad example because it generally needs to be declared as a friend.
This is a (unrecommended) pathological way to do:
A better method is to make a virtual method in the interface, call it something like write(std::ostream&), and then make a operator<< overload
You''re getting lazy - just try stuff!
This is a (unrecommended) pathological way to do:
#include <stdlib.h> #include <iostream> struct ISerializable { virtual std::ostream& operator>>(std::ostream&)=0; };struct look : ISerializable { int i; virtual std::ostream& operator>>(std::ostream& os) { os << "look" << endl; return os; } }; int main() { ISerializable* ser = new look; *ser >> std::cout; system("pause"); }
A better method is to make a virtual method in the interface, call it something like write(std::ostream&), and then make a operator<< overload
#include <stdlib.h>#include <iostream>struct ISerializable { virtual void write(std::ostream&)=0; };struct look : ISerializable { virtual void write(std::ostream& os) { os << "look" << endl; } };std::ostream& operator<<(std::ostream& os, ISerializable& ser) { ser.write(os); return os; }int main() { ISerializable* ser = new look; std::cout << *ser; system("pause"); }
You''re getting lazy - just try stuff!
Okay. Thanks.
PaulWendt of CodeGuru posted an interesting solution from Scott Meyer''s More Effective C++. The solution includes the use of an inline function calling a virtual function in the base class. Here is the link.
http://www.codeguru.com/forum/showthread.php?s=&threadid=209653
PaulWendt has me interested in Scott Meyers'' More Effective C++. Is it a good C++ book? I have read C++ How to Program by Deitel&Deitel, The C++ Standard Library by Nicolai Josuttis , and The C++ Programming Language by Bjarne Stroustrup. Nonetheless, I hear recommendations for More Effective C++, but I have not given much thought to it until now.
Kuphryn
PaulWendt of CodeGuru posted an interesting solution from Scott Meyer''s More Effective C++. The solution includes the use of an inline function calling a virtual function in the base class. Here is the link.
http://www.codeguru.com/forum/showthread.php?s=&threadid=209653
PaulWendt has me interested in Scott Meyers'' More Effective C++. Is it a good C++ book? I have read C++ How to Program by Deitel&Deitel, The C++ Standard Library by Nicolai Josuttis , and The C++ Programming Language by Bjarne Stroustrup. Nonetheless, I hear recommendations for More Effective C++, but I have not given much thought to it until now.
Kuphryn
Magmai Kai Holmlor:
I studied two possible solutions. I found your solution to be most suitable.
Thanks again,
Kuphryn
I studied two possible solutions. I found your solution to be most suitable.
Thanks again,
Kuphryn
The first one, Effective C++ would probably be a good book for you to get, Effective STL is also useful.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement