473,503 Members | 5,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator= for derived class

struct A{
A& operator=( const A& a ){ i = a.i; return *this; }

private:
int i;
};

struct B : public A{
B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?

private:
int j;
};

In the above code, what would be the standard way of coping A::i in
B::operator= ?
Jul 22 '05 #1
3 1688
MojoRison wrote:
struct A{
A& operator=( const A& a ){ i = a.i; return *this; }

private:
int i;
};

struct B : public A{
B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?

private:
int j;
};

In the above code, what would be the standard way of coping A::i in
B::operator= ?

For the classes in their presented form, there is no need for operator=.
But you probably already knew that.

The simple way is to call the A::operator= directly inside B::operator= :

B& operator=(const B& b) { A::operator=(b); j = b.j; return *this }

Victor
Jul 22 '05 #2
MojoRison wrote:
struct A{
A& operator=( const A& a ){ i = a.i; return *this; }

private:
int i;
};

struct B : public A{
B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?

private:
int j;
};

In the above code, what would be the standard way of coping A::i in
B::operator= ?


Firstly, for such classes you don't need to implement the copy
assignment operator explicitly. The one automatically provided by the
compiler will do exactly what you want.

If you still want to implement these operators manually, then the proper
way to do it would be calling the inherited copy assignment operator
('A::operator=') from B's implementation

B& operator=( const B& b ) {
A::operator=(b);
j = b.j;
return *this;
}

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #3
MojoRison wrote:
struct A{
A& operator=( const A& a ){ i = a.i; return *this; }

private:
int i;
};

struct B : public A{
B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?

private:
int j;
};

In the above code, what would be the standard way of coping A::i in
B::operator= ?


I would make the operator= in the parent class
protected. This would prevent "slicing", where only the
parent portion is copied. See [1] below.

One method of implementing assignment operator in child
classes is:
Child&
Child ::
operator=(const Child& ch)
{
if (this != &child) /* guard against self assignment */
{
(Parent&)(*this) = (Parent&)(ch);
/* assignment of child members */
}
return *this;
}
The above fragment comes from Effective C++ or More Effective C++
by Scott Meyers.

[1] When parents allow public assignment, one can assign the
parent portion of child A to child B, using pointers to
the parent class.
struct Parent
{
Parent& operator=(const Parent& p);
};

struct Son : Parent
{
Son& operator=(const Son& s);
};

struct Daughter : Parent
{
Daughter& operator=(const Daughter& d);
};

/* ... */

Son bill;
Son jack;
Daughter amy;
Parent * p_male = &bill;
Parent * p_female = &amy;
Parent * p_brother = & jack;
*p_brother = *p_male; /* Are the child members copied? */
/* One _might_ think so... */
*p_male = *p_female; /* copies only parent portion */
/* Do you want this allowed? */

This has cost me many hours of debugging, so I've learned
to make assignment operators of base classes as protected.
to prevent the above scenario from occurring.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #4

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

Similar topics

3
8245
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
16
3058
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
2903
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
0
2034
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
11
1951
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
7
1705
by: Jim Langston | last post by:
This is something someone was asking in irc. I really don't need to do this right now, but may have to in the future. The following code is in error (marked). #include <iostream> #include...
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 {...
13
3934
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
17
1602
by: Corey Cooper | last post by:
I have a class for which I needed to create an operator= function. I then use that as a base class for another class, which also needs an operator=. What is the syntax to use in the derived class...
1
6697
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
0
7291
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
7357
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...
1
7012
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
5598
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,...
1
5023
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...
0
4690
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...
0
3180
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...
0
1522
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 ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.