C ++ virtual functions and virtual inheritance polymorphism of summary

Virtual base class

  1. A class can be used both in a virtual base class family, it has also been used as a non-virtual base class.
  2. Object of a derived class virtual base class of the same name produced only a virtual base class subobjects, and to a non-virtual base class generate respective child objects.
  3. Virtual base class subobjects by the constructor is best derived class is initialized by calling the constructor of the virtual base class.
  4. The most derived class refers to the establishment of an object in the inheritance hierarchy of the specified class.
  5. A member function of the derived class must be listed in the list of call initialization on the virtual base class constructor; if it is not listed, then the default constructor of the virtual base class.
  6. From the members of the virtual base class constructor is derived directly or indirectly derived class initialization list must be listed in the call to the virtual base class constructor. But only for the constructor of the most derived class object the establishment of virtual base class constructor calls, and calls to the virtual base class constructor of the base class for all derived classes listed are ignored in the implementation process, thus ensuring of the virtual base class subobject initialized only once.
  7. When a call to a non-virtual base class and the virtual base class constructor initializes occur simultaneously in a list of members, the constructor virtual base class constructor is performed prior to the non-virtual base class.

Virtual function

  1. Virtual functions non-static, non-inline member function, not a friend function, but may be a virtual function declared friend function in another class.
  2. Virtual function declarations are only allowed in a function prototype declaration in the class definition, and can not be declared when the function of the body member functions.
  3. A virtual function is public inheritance regardless of how many times, it still retains its characteristic virtual functions.
  4. If the class member functions are illustrated as a virtual function, the member function in the derived class may have different implementations. When the member function using the pointer or reference to the object identified, the call can be dynamic binding member function.
  5. After defining the virtual function pointer to the base class can be declared in the program directed its derived classes. During execution, the function can continue to change the object to which it points, different versions of the member function calls, and these actions are implemented dynamically at runtime. Virtual function polymorphism fully reflects the dynamic object-oriented programming. Pure virtual function version of the member function, and these actions are implemented dynamically at runtime. Virtual function polymorphism fully reflects the dynamic object-oriented programming.

Pure virtual function

  1. When implemented not give a meaningful for the virtual function in the base class, which can be declared as a pure virtual function, derived classes left to complete its implementation.
  2. Action pure virtual function is to provide a consistent interface to the derived class.
  3. Pure virtual function of not materialized, it can declare the pointer.
Published 31 original articles · won praise 42 · views 10000 +

Guess you like

Origin blog.csdn.net/Flame_alone/article/details/104259371