473,778 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ 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 4966

"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::operat or=(const Base&)

Derived::operat or=(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::opera tor=(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
3745
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 would I proceed with operator=? Do I need to supply 2 operators:
15
1909
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
3091
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 { public: virtual int f() = 0; private: int a; };
11
3437
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 exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
7
2116
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 BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
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 { virtual Base& operator = (const Base &k) {} };
17
3548
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;" instead? I think declaring a function as "=0" is the same
8
2539
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
2344
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 hash string class ) are derived. The two derived classes are template classes specifying the sizes. The base class is a non-template class so that it can be used generically in the interface classes. the design would look like
12
2541
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 special. 1 single root class tree 2 the leaf(lowest level) classes are sealed which means we should not inherite class from them. 3 PImpl idiom. There is only one data mumber in root class and there is no any other data mumber in child class and...
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10298
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9923
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8957
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5370
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5500
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4033
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 we have to send another system

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.