Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default operator=() for a derived class?

Reply
Thread Tools

Default operator=() for a derived class?

 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      03-04-2011

Dear C++ community,

You know how C++ classes will automatically provide a default
operator=() method that calls each of its member variables'
operator=() ?

Well, I was wondering: Does the default operator=() method of a
derived class call operator=() on each of the base class' member
variables? Or does it just call the base class' opeator=() for those,
while still using operator=() on its remaining member variables?

Here's an example program to illustrate:

(Note that the Base class prepends "copy of" when copying member
variables, while the Derived class does not have an operator=() method
explicitly defined.)

=== START OF CODE ===

#include <iostream>
#include <string>

class Base
{
public:
std::string a;
std::string b;

Base& operator=(const Base & that)
{
a = "copy of " + that.a;
b = "copy of " + that.b;
return *this;
}

virtual std::string toString()
{
return "a='" + a + "'; b='" + b + "'";
}
};

class Derived : public Base
{
public:
std::string c;

virtual std::string toString()
{
return Base::toString() + "; c = '" + c + "'";
}
};


int main(int argc, char ** argv)
{
Derived var1; var1.a = "a"; var1.b = "b"; var1.c = "c";
Derived var2; var2.a = "x"; var2.b = "x"; var2.c = "x";

var2 = var1;

std::cout << var2.toString() << std::endl;

return 0;
}

=== END OF CODE ===

What should this program print? When I run it, I see this:

a='copy of a'; b='copy of b'; c = 'c'

which implies that the default Derived:perator=() method called
Base:perator=() and also invoked std::string:perator=() on
variable c. If Base:perator=() wasn't used, then we wouldn't see
"copy of" in front of a and b's values.

This makes sense (to me) as default behavior for the operator=()
method of a derived class. My main question is: Is this behavior
explicitly stated in the C++ specifications? (I want to avoid
undefined behavior.)

(I tried searching for this, and while I found similar examples, I
was not able to verify if this is explicitly correct behavior.)

Thanks.

-- Jean-Luc
 
Reply With Quote
 
 
 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      03-07-2011
> jl_p...@hotmail.com wrote in news:292997f1-fd3f-4bec-a5d7-088444497674
> @a21g2000prj.googlegroups.com:
>
> > * *Well, I was wondering: *Does the default operator=() method of a
> > derived class call operator=() on each of the base class' member
> > variables? *Or does it just call the base class' opeator=() for those,
> > while still using operator=() on its remaining member variables?



On Mar 4, 3:20*pm, Paavo Helde <myfirstn...@osa.pri.ee> replied:
> Yes, the latter. This is specified in 12.8/13:
>
> "The implicitly-defined copy assignment operator for class X performs
> memberwise assignment of its subobjects. The direct base classes of X are
> assigned first, in the order of their declaration in the base-
> specifierlist, and then the immediate nonstatic data members of X are
> assigned, in the order in which they were declared in the class
> definition."



Thanks, Paavo. That's exactly the kind of answer I was looking
for.

-- Jean-Luc
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default ctor and derived classes utab C++ 2 02-08-2008 06:05 PM
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57