473,379 Members | 1,491 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,379 software developers and data experts.

operator=() in base and derived class

Dear members of this group,

recently I came across a problem with repsect to operator=() and
inheritance. Consider the following code snippet:

------------------------------------------------------------
#include <iostream>

using namespace std;

class A
{
public:
A & operator=(const A &_a) {
cout << " in A" << endl;
}
};

class B : public A
{
public:
B & operator=(const A &_a) {
cout << " in B" << endl;
A::operator=(_a);
}
};

main()
{
B b1, b2;
A a;

b1=b2;

cout << endl;

b1=a;
}
----------------------------------------

If you run this program (compiled using gcc 3.3 and 4.1), the output
you get is:

-------------------
in A

in B
in A
------------------

This means:
* for the assignment b1=b2, A::operator=() is invoked
* for b1=a, B::operator=() is invoked.

Now the solution to make the code behave as intended by me is to add
another function

B & operator=(const B&_B) {
cout << " in B" << endl;
A::operator=(_b);
}

However, I don't understand the reasons for this behaviour and I'd
like to understand that. What are the rationales behind that behaviour?

Oct 22 '07 #1
7 11665
In b1=b2, since you haven't provided an operator =() in class B that
takes an object of B, you get a default one from compiler which would
do bitwise copy of B's members. Since you have provided an operator
=() in A, the compiler generated operator =() calls this one to copy
A's members. Hence you see a call to A::operator=().

In b1=a, you have your own operator =() that takes object of class A
and hence this is the function that gets called.

-Uday Bidkar
MWimmer wrote:
Dear members of this group,

recently I came across a problem with repsect to operator=() and
inheritance. Consider the following code snippet:

------------------------------------------------------------
#include <iostream>

using namespace std;

class A
{
public:
A & operator=(const A &_a) {
cout << " in A" << endl;
}
};

class B : public A
{
public:
B & operator=(const A &_a) {
cout << " in B" << endl;
A::operator=(_a);
}
};

main()
{
B b1, b2;
A a;

b1=b2;

cout << endl;

b1=a;
}
----------------------------------------

If you run this program (compiled using gcc 3.3 and 4.1), the output
you get is:

-------------------
in A

in B
in A
------------------

This means:
* for the assignment b1=b2, A::operator=() is invoked
* for b1=a, B::operator=() is invoked.

Now the solution to make the code behave as intended by me is to add
another function

B & operator=(const B&_B) {
cout << " in B" << endl;
A::operator=(_b);
}

However, I don't understand the reasons for this behaviour and I'd
like to understand that. What are the rationales behind that behaviour?
Oct 22 '07 #2
wyg
On Oct 22, 4:44 pm, MWimmer <michael.wimm...@gmx.dewrote:
Dear members of this group,

recently I came across a problem with repsect to operator=() and
inheritance. Consider the following code snippet:

------------------------------------------------------------
#include <iostream>

using namespace std;

class A
{
public:
A & operator=(const A &_a) {
cout << " in A" << endl;
}

};

class B : public A
{
public:
B & operator=(const A &_a) {
cout << " in B" << endl;
A::operator=(_a);
}

};

main()
{
B b1, b2;
A a;

b1=b2;

cout << endl;

b1=a;}

----------------------------------------

If you run this program (compiled using gcc 3.3 and 4.1), the output
you get is:

-------------------
in A

in B
in A
------------------

This means:
* for the assignment b1=b2, A::operator=() is invoked
* for b1=a, B::operator=() is invoked.

Now the solution to make the code behave as intended by me is to add
another function

B & operator=(const B&_B) {
cout << " in B" << endl;
A::operator=(_b);
}

However, I don't understand the reasons for this behaviour and I'd
like to understand that. What are the rationales behind that behaviour?
I copied some information from MSDN for you reference:
The assignment operator has some additional restrictions. It can be
overloaded only as a nonstatic member function, not as a friend
function. It is the only operator that cannot be inherited; a derived
class cannot use a base class's assignment operator.
Oct 22 '07 #3
On Oct 22, 11:24 am, Uday Bidkar <uday.bid...@gmail.comwrote:
In b1=b2, since you haven't provided an operator =() in class B that
takes an object of B, you get a default one from compiler which would
do bitwise copy of B's members. Since you have provided an operator
=() in A, the compiler generated operator =() calls this one to copy
A's members. Hence you see a call to A::operator=().

In b1=a, you have your own operator =() that takes object of class A
and hence this is the function that gets called.

-Uday Bidkar
Ah, thanks a lot, that makes it clear!

Oct 22 '07 #4
On Oct 22, 4:44 am, MWimmer <michael.wimm...@gmx.dewrote:
Dear members of this group,

recently I came across a problem with repsect to operator=() and
inheritance. Consider the following code snippet:

------------------------------------------------------------
#include <iostream>

using namespace std;

class A
{
public:
A & operator=(const A &_a) {
cout << " in A" << endl;
}

};

class B : public A
{
public:
B & operator=(const A &_a) {
cout << " in B" << endl;
A::operator=(_a);
}

};

main()
{
B b1, b2;
A a;

b1=b2;

cout << endl;

b1=a;}

----------------------------------------

If you run this program (compiled using gcc 3.3 and 4.1), the output
you get is:

-------------------
in A

in B
in A
------------------

This means:
* for the assignment b1=b2, A::operator=() is invoked
* for b1=a, B::operator=() is invoked.

Now the solution to make the code behave as intended by me is to add
another function

B & operator=(const B&_B) {
cout << " in B" << endl;
A::operator=(_b);
}

However, I don't understand the reasons for this behaviour and I'd
like to understand that. What are the rationales behind that behaviour?
The compiler will implicily create an assignment operator, that just
calls assignment for member and base.
The only way not to get this is to define one yourself, that takes the
type as an argument. In this case, B&operator=(A const&); does not
turn off this implictly generated function.

The complier made up a function for you that looks like
B&operator=(B const&b){
A::operator=(b);
}
Why does it do this? This is more to support some coding styles that
are essentially mixtures of C and C++. So in C we may have

struct A{
int g;
char buf[123];
};
struct B{
A a;//C version of inheritance
double d[543];
};
void foo(){
struct B b1,b2;
b1=b2;//C does a implicit memcpy underneath the hood

}
So you can see that the "implicit operator=" is a holdover from C.
when this is converted to C++, it may look like

struct A1{
int g;
std::string buf;
};
struct B1:A1{
std::vector<doubled;
};

void foo1(){
struct B1 b1,b2;
b1=b2;//C++ does a member by member operator= underneath the hood
//because string and vector will not survive a memcpy
}

Lance

Oct 22 '07 #5
My comment will not change behavior of your classes, but aren't you
missing something here?

MWimmer wrote:
class A
{
public:
A & operator=(const A &_a) {
cout << " in A" << endl;
return *this;
}
};

class B : public A
{
public:
B & operator=(const A &_a) {
cout << " in B" << endl;
A::operator=(_a);
return *this;
}
};
Oct 22 '07 #6
On Oct 22, 11:44 am, anon <a...@no.nowrote:
My comment will not change behavior of your classes, but aren't you
missing something here?

MWimmer wrote:
class A
{
public:
A & operator=(const A &_a) {
cout << " in A" << endl;
return *this;
}
};
class B : public A
{
public:
B & operator=(const A &_a) {
cout << " in B" << endl;
A::operator=(_a);
return *this;
}
};
Yes I did miss the return *this - I forgot to add it in this made up
example.

Thanks guys, you've been very helpful, now i understand the problem :)

Oct 22 '07 #7
On Oct 22, 11:24 am, Uday Bidkar <uday.bid...@gmail.comwrote:
In b1=b2, since you haven't provided an operator =() in class B that
takes an object of B, you get a default one from compiler which would
do bitwise copy of B's members.
Not quite. The default operator= does a memberwise copy.
(Otherwise, he wouldn't have seen the message from the operator=
of the base class.) But you're right concerning the important
point here: unless you explicitly declare a "copy assignment"
operator=, the compiler implicitly generates one.

And just for completeness' sake, I'll add that a function
template operator= is never considered "copy assignment", and
will never prevent the compiler from generating its version.
Since you have provided an operator
=() in A, the compiler generated operator =() calls this one to copy
A's members. Hence you see a call to A::operator=().
And if he hadn't provided it, of course, the assignment would
have been illegal.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 23 '07 #8

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

Similar topics

16
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...
5
by: Bob Hairgrove | last post by:
Consider the following: #include <string> class A { public: A( const std::string & full_name , const std::string & display_name) : m_full_name(full_name)
2
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...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
5
by: noone | last post by:
hi. I don't use exceptions much in the embedded world, but for my plugin interface to a hardware MPEG encoder I'd like to, since there are so many places that the crummy kernel driver can do bad...
7
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
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
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
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
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:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.