473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4912

"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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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...

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.