Copy constructor and move constructor

I'm trying to learn C++ 11. I'm confused about copy constructor and move constructor. Can anyone please tell me all the situations when a copy constructor is called and when a move constructor (also when not)? And besides, in exactly which cases the other constructors are used?

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
  class ArrayWrapper
{
private:
	int* _p_vals;
	int _size;
public:
	ArrayWrapper(int n)
		: _p_vals(new int[n])
		, _size(n)
	{ 
		cout << "Normal constructor\n";
	}
	// move constructor
	ArrayWrapper(ArrayWrapper&& other)
		: _p_vals(other._p_vals)
		, _size(other._size)
	{ 
		other._p_vals = NULL;
		other._size = 0;

		cout << "Inside move constructor\n";
	}
	// copy constructor
	ArrayWrapper(const ArrayWrapper& other)
		: _p_vals(new int[other._size])
		, _size(other._size)
	{
		for (int i = 0; i < _size; ++i)
		{
			_p_vals[i] = other._p_vals[i];
		}
		cout << "Inside copy constructor\n";
	}
	~ArrayWrapper()
	{
		delete []_p_vals;
	}
};

Please tell me with examples for this class...
Last edited on
When the source of the copy is an rvalue - move constructor.
Otherwise, copy constructor.

However when the source of the copy is known to be an rvalue, one can avoid the potentially expensive clone() operation by pilfering the rvalue's pointer (no one will notice!). The move constructor above does exactly that
http://www.artima.com/cppsource/rvalue.html


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
#include <iostream>

struct A
{

    A() = default ;
    explicit A(int) { std::cout << "constructor(int)\n" ; } ;

    ~A() = default ;

    A( const A& ) { std::cout << "lvalue-reference: copy constructor\n" ; }
    A( A&& ) noexcept { std::cout << "rvalue-reference: move constructor\n" ; }

    A& operator= ( const A& ) = default ; // explicitly defaultef copy assignment
    A& operator= ( A&& ) noexcept = default ; // explicitly defaultef move assignment
};

A foo() { A a(234) ; return a ; }

A bar() { A a ; return std::move(a) ; }

const A& foobar() { static A a ; return a ; }

int main()
{
    A a1 ;

    A a2(a1) ; // lvalue: copy constructor

    A a3 = foo() ; // copy elision: constructor(int)
    A a4 = bar() ; // rvalue: move constructor
    A a5 = foobar() ; // lvalue: copy constructor

    A a6( std::move(a1) ) ; // rvalue (xvalue): move constructor

    A&& a7 = A() ;
    A a8(a7) ; // lvalue (has a name): copy constructor
}

http://coliru.stacked-crooked.com/a/a3136b22237c38fd

More information: http://thbecker.net/articles/rvalue_references/section_01.html
A a3 = foo() ; // copy elision: constructor(int)

what is a copy elision?
And what is std::move?
Copy elision: http://en.cppreference.com/w/cpp/language/copy_elision

std::move()
Page 4 of the article linked to earlier: http://thbecker.net/articles/rvalue_references/section_04.html
Consider reading the entire article.

cppreference: http://en.cppreference.com/w/cpp/utility/move
I'm sorry to say that these are all USELESS!
These are written as difficult as possible(especially cppreference), with no good, complete, explanatory examples. No one can start learning from here, only an expert can understand these.
Topic archived. No new replies allowed.