Click to See Complete Forum and Search --> : Polymorphism & Overloaded Operators :: C++


kuphryn
September 13th, 2002, 08:41 PM
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.


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

PaulWendt
September 14th, 2002, 10:30 AM
Yeah, but you should do something that seems totally obvious
AFTER you hear about it :) This is from Item 25 of Scott Meyers'
book "More Effective C++":


...
Neither of these solutions is very satisifying. What we want is a
non-member function called operator<< that exhibits the
behavior of a virtual function like print. The description of what we
want is in fact very close to a description of how to get it. We
define BOTH operator<< and print and have the former call the
latter!


class NLComponent {
public:
virtual ostream& print(ostream& s) const = 0;
...
};

class TextBlock : public NLComponent {
public:
virtual ostream& print(ostream& s) const;
...
};

class Graphic : public NLComponent {
public:
virtual ostream& print(ostream& s) const;
};

inline ostream& operator<<(ostream& s, const NLComponent& c)
{
return c.print(s);
}



Virtual-acting non-member functions, then, are easy. You write
virtual functions to do the work, then write a non-virtual function
that does nothing but call the virtual function. To avoid incurring
the cost of a function call for this syntactic sleight-of-hand, of
course, you inline the non-virtual function



--Paul

kuphryn
September 14th, 2002, 11:59 AM
Wow!!!!!!!!!!! Nice!!!!!

Lets slow down and start from the beginning. The code is very clean and effective. Note I have heard recommendations for Scott Meyers' More Effective C++, but I have read it. However, after seeing your post I am beginning to take interest in it.

Does the inline function belong to the client part of the program?


inline ostream& operator<<(ostream& s, const NLComponent& c)
{
return c.print(s);
}


Lastly, is Scott Meyers' More Effective C++ 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.

Thanks,
Kuphryn

PaulWendt
September 14th, 2002, 01:19 PM
When I took C++ in college, they made us buy two books:
1) Deitel and Deitel's second edition C++ book
2) Bjarne Stroustrup's The C++ Language 2nd Edition

So we have a similar background. I've found the Scott Meyers
series to be VERY helpful. I still haven't mastered all of his
concepts, but you can read these books all the way through and
then use them as reference materials [like oh I remember reading
about this in Effective C++ so I'll go look it up] :) I'm not saying
they'll be helpful for everyone, but all three of his books were
very helpful for me. They are:
Effective C++
More Effective C++
Effective STL

You can put the inline function in the header file for the
NLComponent class [at least that's where I think I'd put it].

--Paul

kuphryn
September 14th, 2002, 03:01 PM
Okay. Thanks.

Kuphryn