473,490 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

virtual assignment operator/polymorphism question

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
3 4920

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3715
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how...
15
1858
by: Heiner | last post by:
#include <stdio.h> class A { public: virtual A & operator= (const A &); virtual void test(const A &); }; class B : public A
2
3068
by: gyarnell | last post by:
Just ran into a problem upgrading from GCC 2.96 to GCC 3.3.2. There is a bug somewhere, could be in 2.96, could be in 3.3.2, could be in the code I'm compiling. Here's a minimal example class A {...
11
3403
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
7
2098
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
2
614
by: sven.bauer | last post by:
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 {...
17
3510
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
8
2523
by: sun1991 | last post by:
Hi All, I tried the following code, but it did not work as I think: --- using namespace std; namespace { class Fraction { public:
14
2312
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
12
2506
by: feel | last post by:
Hi,All I am sure it's an old question. But I just find a interesting design about this: Polymorphism without virtual function in a C++ class. My solution is for some special case, trust me, very...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.