By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,572 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,572 IT Pros & Developers. It's quick & easy.

virtual assignment operator/polymorphism question

P: n/a
Hi,

could you please help me with the following example?
struct Base
{
virtual Base& operator = (const Base &k) {}
};

struct Derived: public Base
{
// 1
virtual Derived& operator = (const Derived &k) {}

// 2
virtual Base& operator = (const Base &k) {}
};

int main(int argc, char* argv[])
{
Derived *dp = new Derived();
Base *bp = new Derived(*dp);

*bp = *dp;

return 0;
}
Why does the assignment in main lead to a call to the second assignment
operator (// 2) and not to the first one (//1)?

Thank you,
Stephan
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Stephan Kurpjuweit" <St****************@sdm.de> wrote in message
news:ca**********@news.sdm.de...
Hi,

could you please help me with the following example?
struct Base
{
virtual Base& operator = (const Base &k) {}
};

struct Derived: public Base
{
// 1
virtual Derived& operator = (const Derived &k) {}

// 2
virtual Base& operator = (const Base &k) {}
};

int main(int argc, char* argv[])
{
Derived *dp = new Derived();
Base *bp = new Derived(*dp);

*bp = *dp;

return 0;
}
Why does the assignment in main lead to a call to the second assignment
operator (// 2) and not to the first one (//1)?


1) *bp is of static type Base, so the compiler generates a virtual call to
Base::operator=(const Base&).

2) At run-time the dynamic type of *bp is examined, since that is Derived
the actual function that gets called is the overridden version in the
Derived class, i.e. Derived::operator=(const Base&)

Derived::operator=(const Derived&) never gets considered at step 1 because
there is no similar function in the Base class, so it cannot be called at
step 2.

john
Jul 22 '05 #2

P: n/a
Stephan Kurpjuweit wrote:
Hi,

could you please help me with the following example?
struct Base
{
virtual Base& operator = (const Base &k) {}
};

struct Derived: public Base
{
// 1
virtual Derived& operator = (const Derived &k) {}

// 2
virtual Base& operator = (const Base &k) {}
};

int main(int argc, char* argv[])
{
Derived *dp = new Derived();
Base *bp = new Derived(*dp);

*bp = *dp;

return 0;
}
Why does the assignment in main lead to a call to the second assignment
operator (// 2) and not to the first one (//1)?


Because // 1 does not override the assignment operator in Base, but the
// 2 does.

V
Jul 22 '05 #3

P: n/a
Stephan Kurpjuweit wrote:
Hi,

could you please help me with the following example?
struct Base
{
virtual Base& operator = (const Base &k) {}
};

struct Derived: public Base
{
// 1
virtual Derived& operator = (const Derived &k) {}

// 2
virtual Base& operator = (const Base &k) {}
};

int main(int argc, char* argv[])
{
Derived *dp = new Derived();
Base *bp = new Derived(*dp);

*bp = *dp;

return 0;
}
Why does the assignment in main lead to a call to the second assignment
operator (// 2) and not to the first one (//1)?
...


Because selection of candidate functions for overload resolution is
based on _static_ type of the object. In this case static type of
left-hand side of the assignment is 'Base'. This means that only
'Base::operator=(const Base&)' is considered (and, therefore, chosen) by
overload resolution.

Now, since 'Base::operator=(const Base&)' is declared as 'virtual', the
choice of actual function to call will be based on _dynamic_ type of the
object. In this case dynamic type of left-hand side of the assignment is
'Derived', which means that 'Derived::operator=(const Base&)' will be
called.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.