Default operator=() for a derived class?

J

jl_post

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::eek:perator=() method called
Base::eek:perator=() and also invoked std::string::eek:perator=() on
variable c. If Base::eek: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
 
J

jl_post

(e-mail address removed) wrote in news:292997f1-fd3f-4bec-a5d7-088444497674
@a21g2000prj.googlegroups.com:



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top