Archived

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

Inheritance and operator overloading

This topic is 5593 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 have a simple class: class CTest { public: CTest(){} virtual ~CTest(){} void operator = (CTest* a) { int a = 2; } } class CTest1 : public CTest { public: CTest1(){} virtual ~CTest1(){} } If i have 2 objects CTest1 (o1 and o2) and i want to use o1 = o2, it never use the overload operator = Why ? I tried differents solutions without success Thanks for your help.

Share this post


Link to post
Share on other sites
You are implementing the operator = to take a pointer to an object and not a (reference to a) object. Try this instead:

  
class CTest
{
int miValue;
public:
CTest& operator=(const CTest& rOther)
{
miValue = rOther.miValue;
return *this;
}
};
[source]

Share this post


Link to post
Share on other sites
The equals operator is not inherited by derived classes. This is a feature of the C++ standard, and there''s good reasons for doing it like that, which I might go into in more details some other time

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites