virtual assignment operator/polymorphism question

S

sven.bauer

Hi,

I have a question following up the following slightly older posting:
http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b

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


class Derived: public Base
{
virtual Base& operator = (const Base &k) {}
};


int main(int argc, char* argv[])
{
Derived *dp1 = new Derived();
Derived *dp2 = new Derived();

*dp1 = *dp2; // Base::eek:perator= is called

Base *bp = *dp1;

*bp = *dp2; // Derived::eek:perator= is called

return 0;
}

While it seems clear to me why *bp = *dp2 leads to the
Derived::eek:perator= being called I do not understand why *dp1 = *dp2
calls the Base::eek:perator=.
What's going on here???

Thanks!

Sven
 
G

Gianni Mariani

Hi,

I have a question following up the following slightly older posting:
http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b

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


class Derived: public Base
{
virtual Base& operator = (const Base &k) {}
};


int main(int argc, char* argv[])
{
Derived *dp1 = new Derived();
Derived *dp2 = new Derived();

*dp1 = *dp2; // Base::eek:perator= is called

Base *bp = *dp1;

*bp = *dp2; // Derived::eek:perator= is called

return 0;
}

While it seems clear to me why *bp = *dp2 leads to the
Derived::eek:perator= being called I do not understand why *dp1 = *dp2
calls the Base::eek:perator=.
What's going on here???

*dp1 = *dp2; calls the compiler supplied operator = i.e.

Derived & operator = (const Derived & k)

The compiler supplied Derived::eek:perator = calls Base::eek:perator=
statically, not dynamically. Hence, it appears you're calling
Base& operator = (const Base &k), but you're not really.


Next time, please post code that compiles.

#include <iostream>

class Base
{
public:
virtual Base& operator = (const Base &k)
{
std::cout << "Base\n"; return *this;
}
};


class Derived: public Base
{
public:
virtual Derived& operator = (const Base &k)
{
std::cout << "Derived\n"; return *this;
}

Derived& operator = (const Derived &k)
{
( * static_cast<Base *>( this ) ) = k;
return *this;
}
};


int main(int argc, char* argv[])
{
Derived *dp1 = new Derived();
Derived *dp2 = new Derived();

*dp1 = *dp2; // Base::eek:perator= is called
// Derived & operator = (const Derived &k);

Base *bp = dp1;

*bp = *dp2; // Derived::eek:perator= is called

return 0;
}
 
J

James Kanze

I have a question following up the following slightly older
posting:http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e5...
class Base
{
virtual Base& operator = (const Base &k) {}
};
class Derived: public Base
{
virtual Base& operator = (const Base &k) {}
};
int main(int argc, char* argv[])
{
Derived *dp1 = new Derived();
Derived *dp2 = new Derived();
*dp1 = *dp2; // Base::eek:perator= is called

How do you know? I think rather that the compiler generated
derived operator is being called (statically, since it isn't
virtual). This operator, of course, calls the base operator=.
Base *bp = *dp1;
*bp = *dp2; // Derived::eek:perator= is called

Yes, but not the default Derived::eek:perator=.
return 0;
}
While it seems clear to me why *bp = *dp2 leads to the
Derived::eek:perator= being called I do not understand why *dp1 = *dp2
calls the Base::eek:perator=.
What's going on here???

Your Derived::eek:perator= is not a copy assignment operator (since
it doesn't assign a Derived to Derived, but a Base to Derived),
so it doesn't inhibit the compiler from generating its version.

More generally, polymorphism and assignment don't work well
together. If you plan on using a class as a base, it's
generally best to declare the operator= private, and not
implement it (or to derive it from something like
boost::noncopyable).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top