473,386 Members | 1,708 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,386 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 2012
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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,...
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
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...

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.