constructors and destructor

Apr 1, 2015 at 8:43am
Hi, I'm having problems understanding this 1 example. Author of the book that I'm reading suggested to run this program to see in what order all kinds of constructors and destructor are called.

There are more examples in code but i did't understand this one.

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
struct X{

	int val;

//----------------------------------------------------------------
	//default constructors
	X(){ val = 0;  
	cout << "X() default\n"; 
	*p_ofs << "X() default\n";
	}
	X(int v) { val = v; 
	cout << "X(int) default\n";
	*p_ofs << "X(int) default\n";
	}
//----------------------------------------------------------------
	//move constructor
	X(X&& x) {
		val = x.val;
		cout << "X(X&& x) move constructor\n";
		*p_ofs << "X(X&& x) move constructor\n";
	}
	//move assignment
	X& operator=(X&& x){
		val = x.val;

		cout << "X& operator=(X&& x) move assignment\n";
		*p_ofs << "X& operator=(X&& x) move assignment\n";
		return *this;
	}
//----------------------------------------------------------------
	//copy constructor    X loc2{ loc };
	X(const X& x){ val = x.val; 
	cout<<"X(X&) copy constr\n";
	*p_ofs << "X(X&) copy constr\n";
	}
	//copy assignment     loc = X{ 5 };
	X& operator=(const X& a){
		cout<<"X::operator=() copy assignm\n"; 
		*p_ofs << "X::operator=() copy assignm\n";
		val = a.val; 
		return *this;
	}
//----------------------------------------------------------------
	//destructor
	~X(){ 
	cout << "~X() destructor\n";
	*p_ofs << "~X() destructor\n";
	}
	
};


1
2
3
4
X copy(X a) { 
	*p_ofs << "------ Before return a; ------" << endl;
	return a; 
}


1
2
3
4
int main(){
    *p_ofs << "\tloc2 = copy(loc); \n\n";
    loc2 = copy(loc);    //call by value and return
}


Output i was getting is this :

        loc2 = copy(loc); 
        
X(X&) copy constr
------ Before return a; ------
X(X&& x) move constructor
~X() destructor
X& operator=(X&& x) move assignment
~X() destructor

1. I understand that we call copy constructor to initialize (X a) from loc
2. is my output line inside copy funton
3. I'm having problems understanding this line
X(X&& x) move constructor
We just created a variable and the next thing we are trying to do is return this (already initialized) a. Is it because we have to move this a from argument list to a in line where is return a;?

I would understand output like this
X(X&) copy constr
------ Before return a; ------
X& operator=(X&& x) move assignment
~X() destructor

Last edited on Apr 1, 2015 at 8:45am
Apr 1, 2015 at 10:40am
Yes, "return a;" is creating another unnamed temporary value.
Think what happens if "a" was taken by reference instead: if it was, and no unnamed temporary variable was getting constructed, you were going to miss a copyconstruction (causing your function to implicitly return a reference, or even worse, a rvalue reference, acting like std::move, creating more confusing behaviour).
Last edited on Apr 1, 2015 at 10:44am
Apr 1, 2015 at 3:41pm
Thanks a lot for answer! :)

So 2 destructors are for
1. deleting x that was in the argument list,
2. deleting another x that was created (by move constructor) to return x; value?
Last edited on Apr 1, 2015 at 3:42pm
Apr 1, 2015 at 8:01pm
Ok, case solved, thank you very much S G H :)
Apr 2, 2015 at 3:24am
Sorry if I couldn't answer in time.
Yes, every object that has been constructed gets destructed.
You have two constructed objects (copy constr+move constr), so you will see two destructed objects as well.
Topic archived. No new replies allowed.