C++ copy costructor -
im trying grasp copy constructors & i've found part of code.
#include<iostream> using namespace std; class a1 { int data; public: a1(int = 10) : data(i) { cout << "i constructing a1 with: " << << endl; } a1(const a1& a1) : data(a1.data) { cout << "i copy constructing a1" << endl; } ~a1() { cout << "i destroying a1 with: " << data << endl; } void change() { data = data * 10; } }; class a2 { int data; public: a2(int = 20) : data(i) { cout << "i constructing a2 with: " << << endl; } a2(const a2& a2) : data(a2.data) { cout << "i copy constructing a2" << endl; } ~a2() { cout << "i destroying a2 with: " << data << endl; } void change() { data = data * 20; } }; class a3 { public: a3() { cout << "i constructing a3" << endl; } a3(const a3& a3) { cout << "i copy constructing a3" << endl; } ~a3() { cout << "i destroying a3" << endl; } void change() { cout << "nothing change" << endl; } }; class { a1 a1; a2 a2; a3 a3; public: a() { cout << "i constructing a" << endl; } a(const a& a) : a1(a.a1) { cout << "i copy constructing a" << endl; } ~a() { cout << "i destroying a" << endl; } a& operator=(const a& a) { cout << "i performing stupid assignment between as" << endl; if (this != &a) a1 = a.a1; return *this; } void change() { a1.change(); a2.change(); a3.change(); } }; class biga { data1; a& data2; public: biga(a& a) : data1(a), data2(a) { cout << "i constructed biga" << endl; } ~biga() { cout << "i destroying biga" << endl; } get(int index) { if (index == 1) return data1; else return data2; } }; biga volta(biga& biga) //biga& volta(biga& biga) { cout << "volta ta data?" << endl; return biga; } int main() { first; biga biga(first); volta(biga).get(2).change(); return 0; }
however,i can't understand why these results.especially,why a1 , copy constructor called , not constructor , don't @ when volta function called(the results enclosed ****) :
i constructing a1 with: 10 constructing a2 with: 20 constructing a3 constructing copy constructing a1 constructing a2 with: 20 constructing a3 copy constructing constructed biga **** volta ta data? copy constructing a1 constructing a2 with: 20 constructing a3 copy constructing copy constructing a1 constructing a2 with: 20 constructing a3 copy constructing nothing change destroying destroying a3 destroying a2 with: 400 destroying a1 with: 100 destroying biga destroying destroying a3 destroying a2 with: 20 destroying a1 with: 10 **** destroying biga destroying destroying a3 destroying a2 with: 20 destroying a1 with: 10 destroying destroying a3 destroying a2 with: 20 destroying a1 with: 10
edit_assignmentoperatorquery : if add function in biga
void change() { a& rdata1 = data1; cdata2 = data2; }
and call main : biga.change();
why instead of default assignment operator , copy-constructor , constructor being called ,
i copy constructing a1 constructing a2 with: 20 constructing a3 copy constructing
edit_answeringmyownquery : found out initialization copy constructor , not assignment assignment operator.
let's start it.
a first;
you create object, fields (non-static members) initialized
i constructing a1 with: 10 constructing a2 with: 20 constructing a3
and version of constructor without parameters being called:
i constructing
when write
biga biga(first);
one of biga
constructors invoked. takes reference a
object, so, first
not copied (reference set when providing value).
then, member initializer lists time comes,
biga(a& a) : data1(a), data2(a)
and data1
of type a
, first
object copied (it referenced here a
)
a new a
object created own copy constructor. @ first, calles copy constructor a1
,
a(const a& a) : a1(a.a1) copy constructing a1
then, a
's a2
, a3
fields default-initialized.
i constructing a2 with: 20 constructing a3
the body of copy constructor a1
executed then:
i copy constructing
let's return biga
initialization. spoke data1
initialization until now, , time a& data2
:
biga(a& a) : data1(a), data2(a)
as reference, , reference being passed initialize it, assignment, no output.
biga
constructor (that takes a&
) body executed then:
i constructed biga
now, try clarify happens on
volta(biga).get(2).change();
this function being called:
biga volta(biga& biga) { cout << "volta ta data?" << endl; return biga; }
again, pass reference results in no copy constructor call.
we have function body being executed:
"volta ta data?"
the function returns unnamed object of class biga
, copy constructor should called.
you have not provided copy constructor biga (const biga & biga)
, default copy constructor being invoked. sequential member initialization of a data1;
, a& data2;
the first member initialized copying data1
field of unnamed object, copy constructor of a
being called. printed here explained above (see: a new a
object created own copy constructor...)
i copy constructing a1 constructing a2 with: 20 constructing a3 copy constructing
then, get
method runs index == 2
a get(int index) { if (index == 1) return data1; else return data2; // <--- line executed
data2
a&
, , method returns a
, leads a
copy constructor execution.
i copy constructing a1 constructing a2 with: 20 constructing a3 copy constructing
at last, change
runs
void change() { a1.change(); a2.change(); a3.change(); }
and a3.change()
prints something:
nothing change
on program termination
the destruction occurs in reverse order, , last created change
'd object destroyed @ first.
i destroying destroying a3 destroying a2 with: 400 destroying a1 with: 100
i destroying biga
printed twice, i constructed biga
- once. latter due have no copy constructor biga
takes const & biga
(it pointed out above).
answering query
void change() { a& rdata1 = data1; cdata2 = data2; } //in main(): biga.change();
yes, you're right copy constructor called here a cdata2 = data2;
because object cdata2
uninitialized. case explained under ref.
if change code that
a cdata2; cdata2 = data2;
you see expected assignment:
i constructing a1 with: 10 constructing a2 with: 20 constructing a3 constructing performing stupid assignment between
Comments
Post a Comment