473,769 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignment Operator Problem on Based and Derived Class

Hello all, I am confused on the correct assignment operator that
should be implemented on the derived class. I refer to the document
found at :-

http://www.icu-project.org/docs/pape..._operator.html

which the author recommence not using "virtual" for assignment
operator. Hence, I provide the following implementation.

#include <iostream>

using namespace std;

class B {
public:
B(int i) : b(i) {}

B& operator=(const B& me) {
if(&me == this) return *this;

b = me.b;

cout<< "b assignment"<< endl; return *this;
}

virtual void fun() { cout<< "b: " << b << endl; }

private:
int b;

};

class D: public B {
public:
D(int i) : B(i), d(i) {}

D& operator=(const D& me) {
if(&me == this) return *this;

d = me.d;

B::operator =(me); cout<< "d assignment"<< endl; return *this;
}

virtual void fun() { B::fun(); cout<< "d: " << d << endl; }

private:
int d;
};

int main()
{
cout<< "main"<< endl;

B* b0 = new D(100);
B* b1 = new D(200);

(*b0).fun(); // "b: 100"
// "d: 100"
// printed. Correct behavior.

*b0 = *b1; // "b assignment" printed. is this the correct
expectation?
// shall we expected "b assignment", followed by "d assignment"
// to be printed? If yes, what shall be the correct
implementation?

(*b0).fun(); // "b: 200"
// "d: 100"
// printed. Now the result is "half-baked". The based class are
// being assigned properly. However, the child class is not

delete b0;
delete b1;
}

It seems that for me, the assignment operator implementation for
derived class is not correct (At least it shall not produce "half-
baked result) May I know what shall the correct implementation for
that? Any suggestion are welcomed.

Thanks!

cheok
Sep 11 '08 #1
1 2040
yccheok wrote:
Hello all, I am confused on the correct assignment operator that
should be implemented on the derived class. I refer to the document
found at :-

http://www.icu-project.org/docs/pape..._operator.html

which the author recommence not using "virtual" for assignment
operator. Hence, I provide the following implementation.

#include <iostream>

using namespace std;

class B {
public:
B(int i) : b(i) {}

B& operator=(const B& me) {
if(&me == this) return *this;

b = me.b;

cout<< "b assignment"<< endl; return *this;
}

virtual void fun() { cout<< "b: " << b << endl; }

private:
int b;

};

class D: public B {
public:
D(int i) : B(i), d(i) {}

D& operator=(const D& me) {
if(&me == this) return *this;

d = me.d;

B::operator =(me); cout<< "d assignment"<< endl; return *this;
}

virtual void fun() { B::fun(); cout<< "d: " << d << endl; }

private:
int d;
};

int main()
{
cout<< "main"<< endl;

B* b0 = new D(100);
B* b1 = new D(200);

(*b0).fun(); // "b: 100"
// "d: 100"
// printed. Correct behavior.

*b0 = *b1; // "b assignment" printed. is this the correct
expectation?
// shall we expected "b assignment", followed by "d assignment"
// to be printed? If yes, what shall be the correct
implementation?

(*b0).fun(); // "b: 200"
// "d: 100"
// printed. Now the result is "half-baked". The based class are
// being assigned properly. However, the child class is not

delete b0;
delete b1;
}

It seems that for me, the assignment operator implementation for
derived class is not correct (At least it shall not produce "half-
baked result) May I know what shall the correct implementation for
that? Any suggestion are welcomed.

class B { ...

virtual B& operator=(B const& b)
{ /* same as you have it */ }
};

class D : public B {
...
virtual D& operator =(B const& b) {
// check if 'b' is actually a D
if (D const* pD = dynamic_cast<D const*>(&b)) {
return *this = *pD;
}
else { // something else, assign only base part???
return B::operator=(b) ;
}
}

virtual D& operator =(D const& d) {
// whatever
}
}

HTH

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 11 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3066
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele, Gary Moore LP, LP Faded DC, Jeff Beck Strat, Morgan OM Acoustic,
3
4964
by: Stephan Kurpjuweit | last post by:
Hi, could you please help me with the following example? struct Base { virtual Base& operator = (const Base &k) {} };
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:
14
2359
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object in this case object b2 below. It also initialize subobject from class SphereClass. The BallClass copy constructor looks like this. BallClass::BallClass(const BallClass& bc) : SphereClass(bc)
7
3915
by: Mr. Ed | last post by:
I have a base class which has about 150 derived classes. Most of the derived classes are very similar, and many don't change the base class at all. All the derived classes have a unique factory method which returns a new object of the derived type. The problem I've got is that I now need to polymorphically clone a derived class object, but I don't want to write a separate 'clone' method for each of these 150 classes. Instead, I thought I...
11
2404
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
7
11027
by: vj | last post by:
Hi Friends, I was going through a C++ reference book when this rule caught my eye: -->Assignment operator '=' is not inherited by the sub class. I cannot figure out why this rule has being imposed and how it actually makes any sense. If operator '=' cannot be inherited then it should not be allowed to be made virtual . But following declaration on VC 6.0 compiler gets through :
2
6248
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for manipulation. Now A has a copy constructor and assignment operator. What will happen if copying goes on in B? Do I need to have an overloaded set of functions which call the same copy functions that A has available? Or are they virtual in the...
12
1919
by: siddhu | last post by:
Dear experts, #include <stdio.h> class Base { }; class Der1:public Base {
0
9579
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
9420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10205
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
8863
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...
1
7401
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
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
5293
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
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
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.