--> 指针 <--
1. 对于void * 类型的指针,建议将其转换为合适的类型后再进行delete操作,否则将可能出现内存泄漏问题。(原因:编译器无法确定void * 确切的类型信息,也就无从知晓该调用哪个类的析构函数,因此,不进行转换只会释放其指向对象的内存空间,而不会调用其指向对象的析构函数)。
2. 对于void * 类型的指针,如果其指向基本数据类型,则可以不对其进行精确的类型转换,并可以实现安全的delete操作。(原因: 不涉及对象的析构函数调用问题)。
3. 指针使用之前,建议初始化为NULL或0。
4. 如果容器内的元素是指针类型,确保在销毁容器时对每个元素进行delete,以防内存泄漏。
5. 指向const的指针和const指针:
(a) const int * var 或 int const * var // 表示var指向的值不可改变;
(b) int * const var; // 表示不能改变var的指向,但var指向的内容可以修改
--> 静态变量 <--
1. 静态局部变量仅在第一次函数调用时进行初始化,后续调用不进行初始化。
2. static修饰的变量和函数具有文件作用域,在文件外部不可访问。
--> 静态类成员变量 <--
1. static data members mush be defined outside the class body. static data members should be initialized when they are defined. One exception to this rule is that a const static data member of integral type can be initialized within the class body as long as the initializer is a constant expression such as: (static const int a = 3 // 3 is constant expression.)
--> 常量 <--
1. const默认为内部链接,仅在定义的文件内可见,此时的const变量必须有初始值;若const变量用extern进行了修饰,
则可以不选择进行初始化,此时的const变量将作为声明出现,其定义在它处
2. 关于const定义和声明的区别:
(a) extern const int var; // 这是声明,表示var在其它地方已经定义;
(b) extern const int var = 1; // 这是定义;
3. 针对同一个变量,const_cast操作只能进行一次,不能进行两次转换
--> 构造函数 <--
1. C++ Primer: We must use an initializer for any const or reference member or for any member of a class type that does not have a default constructor. (对于常成员、引用成员或没有默认构造函数的类成员,必需在构造函数初始化列表中进行初始化)
2. C++ Primer:
The order in which members are initialized is the order in which the members are defined. The first member is initialized first, then the next, and so on.(初始化列表中的初始化顺序以成员声明的顺序为准,第一个声明的成员最先进行初始化,接着是第二个声明的成员进行初始化,以此类推。)
The order of initialization often doesn't matter. However, if one member is initialized in terms of another,then the order in which members are initialized is crucially important.(初始化的顺序并不重要。然而,如果一个成员的初始化依赖另外一个,这就变的很重要了。一点建议:成员定义的顺序与初始化列表的顺序要一致,防止运行时错误出现。)
--> 析构函数 <--
1. 析构函数应声明为virtual,详见点击打开链接
2. 指针成员变量或者容器中含有指针类型的变量均需要手动进行delete,并将对应指针变量置为NULL或0
--> 多态与继承 <--
1. 如果重定义了基类中的一个重载成员函数,则在派生类中其他的重载函数将会被隐藏