472,955 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1994
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
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,...
3
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
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...
14
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...
7
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...
11
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
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...
2
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...
12
by: siddhu | last post by:
Dear experts, #include <stdio.h> class Base { }; class Der1:public Base {
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.