Constructor/Destructor/Copy Constructor/operator =

1) If you have created any constructors or even copy constructors, the compiler won't create default constructor for you.

2) operator =, copy constructor and destructor are siblings, if we overwrite any of them, we should overwrite the rest of them.

3) The order of construction/destruction of class objects. If a class contains a series of objects that belong to different classes, the order of constructions of these objects depends on the order that these objects are defined, not depends on the order in the initialization list of the constructor. The order of destructions of these objects is reversed. i.e. if object A is the first one to be constructed, then it is the last one to be destructed.

4) The order of construction/destruction of base classes. If a class derives from multiple classes, then the order of construction of base classes depends on the order which base class is derived first, not depends on the order in the initialization list. The order of destructions of base classes is reversed. e.g. class C : public A, public B {..}, then constructor of A is called first.

5) The order of construction/destruction between base classes and member objects. The construction of base classes is called first, then the construction of member objects is called. The destruction between base classed and member objects is reversed.

6) The difference between operator = and copy constructor. Copy constructor has no return value while operator = return a reference to a object. Operator = has to check if it is a self-assignment.such as if(this == &other). Operator = has to release resources that have acquired while copy constructor doesn't need to check since it is a kind of construction.

7) If a class contains other class objects, and you don't explicitly call its constructor in the initialized list, compiler will call  the default constructor for this object. If this object doesn't have a default constructor, compiler will complain.

class AAA
{
public:
    AAA(){}// default constructor
    AAA(int i):v(i){cout<<"constructor AAA"<<endl;}
    ~AAA(){cout<<"destructor AAA"<<endl;}
private:
    int v;
};
 

struct BBB
{
	BBB(int i) {cout<<"constructor BBB"<<endl;}
	~BBB(){cout<<"destructor BBB"<<endl;}
};
struct Compound
{
	BBB b;
	AAA a;

//here we don't explicitly call the constructor of class AAA, 
//compiler will try to call the default constructor, if class 
//AAA doesn't have a default constructor, compiler will complain.
       Compound(int i):b(i){}
}
int main()
{
    Compound c(1);
    return 0;
}


6)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值