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:
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
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:
Base:
variable c. If Base:
"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