C++11 Move Constructor

#include <iostream>
#include <vector>
using namespace std;

class A
{
	public:

		float x;

	public:

		A()
		: x(0.f)
		{ cout<<"Default Constructor"<<endl; }

		A( const A& a )
		: x(a.x)
		{ cout<<"Copy Constructor"<<endl; }

		A( const A&& a )
		: x(a.x)
		{ cout<<"Move Constructor"<<endl; }

		A( float _x )
		: x( _x )
		{ cout<<"Class Constructor"<<endl; }

		~A()
		{ cout<<"Destructor"<<endl; }

		A& operator=( const A& a )
		{ cout<<"Assignment Operator"<<endl; x=a.x; return *this; }
};
		

int main( int argc, char* argv[] )
{
	cout << endl << "A a;" << endl;
	{
		A a;
	}

	cout << endl << "A a(1.f);" << endl;
	{
		A a(1.f);
	}

	cout << endl << "A a=A(1.f);" << endl;
	{
		A a=A(1.f);
	}

	cout << endl << "A a; a=A(1.f);" << endl;
	{
		A a; a=A(1.f);
	}

	cout << endl << "A a; vector<A> ar; ar.push_back(a);" << endl;
	{
		A a; vector<A> ar; ar.push_back(a);
	}

	cout << endl << "A a; vector<A> ar; ar.push_back(A(1.f));" << endl;
	{
		A a; vector<A> ar; ar.push_back(A(1.f));
	}

	cout << endl << "A a; vector<A> ar; ar.push_back(move(A(1.f)));" << endl;
	{
		A a; vector<A> ar; ar.push_back(move(A(1.f)));
	}

	cout << endl << "A a; vector<A> ar; ar.emplace_back(1.f);" << endl;
	{
		A a; vector<A> ar; ar.emplace_back(1.f);
	}

	return 0;
}

((RESULTS))

A a;
Default Constructor
Destructor

A a(1.f);
Class Constructor
Destructor

A a=A(1.f);
Class Constructor
Destructor

A a; a=A(1.f);
Default Constructor
Class Constructor
Assignment Operator
Destructor
Destructor

A a; vector<A> ar; ar.push_back(a);
Default Constructor
Copy Constructor
Destructor
Destructor

A a; vector<A> ar; ar.push_back(A(1.f));
Default Constructor
Class Constructor
Move Constructor
Destructor
Destructor
Destructor

A a; vector<A> ar; ar.push_back(move(A(1.f)));
Default Constructor
Class Constructor
Move Constructor
Destructor
Destructor
Destructor

A a; vector<A> ar; ar.emplace_back(1.f);
Default Constructor
Class Constructor
Destructor
Destructor

답글 남기기