c++ constructor

Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class {
public:
Foo(int a) : data_(a) {}


Foo(const Foo& other) {
data_ = other.data_;
}
// copy assignment ctor
Foo& operator=(const Foo& rhs) {
if (*this != rhs) {
data_ = rhs.data_;
}
return *this;
}
~Foo();
private:
int data_;
};

Copy Constructor

Foo(const Foo& other);
Foo(Foo& other);

拷贝构造函数用于使用其他对象的数据来初始化之前未初始化的对象的数据

A a;
A aa=a;

Assignment Constructor

赋值构造函数用于使用其他对象的数据来替换已经初始化的对象的数据

copy assignment constructor

Foo& operator=(Foo& rhs);
Foo& operator=(const Foo& rhs);

A aa;
A a;
aa = a;

move assignment constructor

Move Constructor