C++知识点整理

--> 指针 <--html

    1.  对于void * 类型的指针,建议将其转换为合适的类型后再进行delete操做,不然将可能出现内存泄漏问题。(缘由:编译器没法肯定void * 确切的类型信息,也就无从知晓该调用哪一个类的析构函数,所以,不进行转换只会释放其指向对象的内存空间,而不会调用其指向对象的析构函数)。express

    2.  对于void * 类型的指针,若是其指向基本数据类型,则能够不对其进行精确的类型转换,并能够实现安全的delete操做。(缘由: 不涉及对象的析构函数调用问题)。安全

    3.  指针使用以前,建议初始化为NULL或0。ide

    4.  若是容器内的元素是指针类型,确保在销毁容器时对每一个元素进行delete,以防内存泄漏。函数

    5.  指向const的指针和const指针:this

         (a) const int * var 或 int const * var  // 表示var指向的值不可改变;
spa

         (b) int * const var;  // 表示不能改变var的指向,但var指向的内容能够修改指针


--> 静态变量 <--
htm

    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. 若是重定义了基类中的一个重载成员函数,则在派生类中其余的重载函数将会被隐藏