• Advertisement

Archived

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

operator overloading in derived classes

This topic is 5347 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

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
Advertisement
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
you can use a reference, then the return *this should work.

edit: with casting

[edited by - billybob on July 4, 2003 8:49:02 PM]

Share this post


Link to post
Share on other sites
The problem is I derived it from a DirectX class (D3DXVECTOR2), so I have no control over the code of the operators. I''ll try the constructor.

Share this post


Link to post
Share on other sites

  • Advertisement