Archived

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

alnite

operator overloading in derived classes

Recommended Posts

I got an error telling me I cannot convert from 'BaseClass' to 'DerivedClass' when I use the operator functions defined in BaseClass. I pretty much understand the error. The type of the return value of the operator functions defined in BaseClass is BaseClass, but I use it for variables of type DerivedClass: DerivedClass a, b; DerivedClass sc = b + a; So the conversion error occurs. My question is, I don't know if regular type-casting (DerivedClass) will work here, even if it does work, I don't know if it's safe, and I suspect I have to do it for each operator and variable I use, like this: DerivedClass a, b; DerivedClass sc = (DerivedClass)b + (DerivedClass)a; So, what should I do? Thanks in advance.

Share this post


Link to post
Share on other sites
All overloadable operators, except the copy operator =, should be derived from your base class automatically. Though, you will need an constructor like this in your DerivedClass:

DerivedClass( const BaseClass &b) {...}

[edited by - genne on July 4, 2003 8:49:13 PM]

[edited by - genne on July 4, 2003 8:50:33 PM]

Share this post


Link to post
Share on other sites