Re: default assignment
On 24 Jun., 17:21, jam <farid.mehr...@gmail.com> wrote:
#include<conio.h>
This is some non-standard header.
#include<iostream.h>
Should probably be #include<iostream>, because
<iostream.h> was never part of the official standard.
struct A{
A(){cout<<"start A"<<endl;};
A& operator=(const A&){cout<<"assign A"<<endl;return *this;};
~A(){cout<<"finish A"<<endl;};
};
You did not handle the copy c'tor. Was this an oversight or
by design?
int main()
{
E e1;
endl(cout);
{
E e2=e1;
This one invokes the copy c'tor and thus should not
output anything.
e2=e1;
OK, the copy-assignment operator.
start A
start B
start C
start D
start E
assign A
assign B
assign C
assign D
finish E
finish D
finish C
finish B
finish A
from which we can see that the destructuction order of subobjects/data
members is reverse to that of construction(just as expected ),but is
the behavoir of default assignment operator standard too? I mean is
there any guarantee that a std compiler default assigns subobjects/
data members in the same order as the construction?
Yes, the order of member and base class invokations of the implicitely
generated copy-assignment operator is ruled by the standard. The
corresponding section is [class.copy], p. 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.[..]"
Please note that only virtual base classes have some unspecified
parts of this assignment (I left this part out).
And yes, above rules match the rules for initialization of bases and
members inside any c'tor as described in [class.base.init].
Greetings from Bremen,
Daniel Kr?gler
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]