Article Directory
1 Behavior
Direct method during initialization (parenthesis method): call the copy constructor;
assignment method during initialization: call the copy constructor;
assignment method after initialization: call the assignment operator overload function;
directly initialize the list method (pass the type object in braces): Call the copy constructor;
assign initialization list mode (type object in braces after the equal sign): call the copy constructor;
directly initialize the list mode (pass the member list in braces): call the matching constructor with parameters;
assign initialization list Method (pass the member list in braces after the equal sign): call the matching constructor with parameters;
The direct method of initialization combined with move semantics: call the move copy constructor
, the assignment method during initialization combined with the move semantics: call the move copy constructor
, the assignment method after initialization combined with the move semantics: call the assignment operator overload function;
2 Sample code
#include <iostream>
#include <string>
using namespace std;
class A
{
private:
string name;
int value;
public:
A()
{
value = 1;
name = "aloha";
cout << "call A() this="<<this << endl;
}
A(int value)
{
this->value = value;
cout << "call A(int )" << endl;
}
A(string name,int value)
{
this->name = name;
this->value = value;
cout << "call A(string,int)" << endl;
}
A( const A& a)
{
cout << "call A(A&) this=" << this << endl;
a.show();
this->name = a.name;
this->value = a.value;
}
A& operator=(const A& a)
{
cout << "call =A(A&) this=" << this << endl;
a.show();
this->name = a.name;
this->value = a.value;
return *this;
}
A(const A&& a)
{
cout << "call A(A&&) this=" << this << endl;
a.show();
this->name = a.name;
this->value = a.value;
}
void show()const
{
cout << "object addr=" << this << ",name=" << name << ",value=" << value<<endl;
}
};
int main()
{
A a;
a.show();
cout << endl << "case 1:" << endl;
A a1(a);
a1.show();
cout << endl << "case 2:" << endl;
A a2 = a;
a2.show();
cout << endl << "case 3:" << endl;
A a3;
a3=a;
a3.show();
cout << endl << "case 4:" << endl;
A a4(move(a));
a4.show();
cout << endl << "case 5:" << endl;
A a5 = move(a) ;
a5.show();
cout << endl << "case 6:" << endl;
A a6 ;
a6 = move(a);
a6.show();
cout << endl << "case 7:" << endl;
A a7{
a};
a7.show();
cout << endl << "case 8:" << endl;
A a8 = {
a };
a8.show();
cout << endl << "case 9:" << endl;
A a9 = {
"aloha",5 };
a9.show();
cout << endl << "case 10:" << endl;
A a10 {
"aloha",5 };
a10.show();
}
3 Operation
call A() this=010FFD78
object addr=010FFD78,name=aloha,value=1
case 1:
call A(A&) this=010FFD50
object addr=010FFD78,name=aloha,value=1
object addr=010FFD50,name=aloha,value=1
case 2:
call A(A&) this=010FFD28
object addr=010FFD78,name=aloha,value=1
object addr=010FFD28,name=aloha,value=1
case 3:
call A() this=010FFD00
call =A(A&) this=010FFD00
object addr=010FFD78,name=aloha,value=1
object addr=010FFD00,name=aloha,value=1
case 4:
call A(A&&) this=010FFCD8
object addr=010FFD78,name=aloha,value=1
object addr=010FFCD8,name=aloha,value=1
case 5:
call A(A&&) this=010FFCB0
object addr=010FFD78,name=aloha,value=1
object addr=010FFCB0,name=aloha,value=1
case 6:
call A() this=010FFC88
call =A(A&) this=010FFC88
object addr=010FFD78,name=aloha,value=1
object addr=010FFC88,name=aloha,value=1
case 7:
call A(A&) this=010FFC60
object addr=010FFD78,name=aloha,value=1
object addr=010FFC60,name=aloha,value=1
case 8:
call A(A&) this=010FFC38
object addr=010FFD78,name=aloha,value=1
object addr=010FFC38,name=aloha,value=1
case 9:
call A(string,int)
object addr=010FFC10,name=aloha,value=5
case 10:
call A(string,int)
object addr=010FFBE8,name=aloha,value=5