Polymorphism & Overloaded Operators :: C++

Started by
3 comments, last by kuphryn 21 years, 7 months ago
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
Advertisement
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:

  #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!
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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
Magmai Kai Holmlor:

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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement