Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kuphryn

Polymorphism & Overloaded Operators :: C++

This topic is 5940 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Magmai Kai Holmlor:

I studied two possible solutions. I found your solution to be most suitable.

Thanks again,
Kuphryn

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!