Constructor/Destructor/Copy Constructor/operator =
来源:互联网 发布:人力资源管理知乎 编辑:程序博客网 时间:2024/05/22 13:12
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)
- Constructor/Destructor/Copy Constructor/operator =
- copy constructor,destructor 和copy assignment operator
- default constructor, copy constructor, copy assignment operator, destructor
- copy constructor & operator =
- copy constructor and = operator
- operator=, swap, copy constructor
- Copy Constructor and operator=
- c++ copy constructor, operator=
- 类的三个特殊成员Copy Constructor、Copy-Assignment Operator、Destructor重载与使用
- constructor copy constructor initialization and assignment operator
- [C++]copy constructor and copy assignment operator
- why we need copy constructor & assignment operator
- copy constructor && =
- Default Constructor && Copy Constructor
- constructor and destructor
- GCC __attribute__((constructor)|(destructor))
- constructor and destructor
- _init _fini constructor destructor
- JAVA同步代码块
- 敲不出代码,但是上课听的明白,怎么办?
- html5 游戏研究 参考
- Server 2012 Hyper-v新功能之三:Hyper-V 副本
- 整合discuz X2.5后用户免激活设置
- Constructor/Destructor/Copy Constructor/operator =
- 黑马程序员==》java基础 函数的初步了解
- 关于设置navigationController title 和 TabBarController title 的问题
- Ubuntu 下 Android SDK Manager 被 GFW 无法更新的解决办法
- ORACLE函数大全
- 操作系统——动态优先级调度算法源代码
- Android alertDialog
- STL中的bitset
- asp.net中使用jQuery的ajax--同时维持视图状态