C ++ virtual functions and virtual function table Comments

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/saber_wtq/article/details/90691766

Before explaining virtual functions need to distinguish between the following definition: overloaded, rewrite, redefined

  1. Overloaded --- the same function name in the same class, function parameter list is not the same two and more than two functions is function overloading. Note: The return value of a function as a function of whether or not based on overloaded.
  2. Rewrite --- when the parent class is inherited in subclasses of the parent class virtual functions were covered. Rewriting the program causes the occurrence of dynamic binding, polymorphism is generated.
  3. Redefine --- is inherited from the parent class when the subclasses of the parent class non-virtual functions were covered.

Object-oriented design language has ---- polymorphism.

  • Static Polymorphism: function overloading, operator overloading
  • Dynamic Polymorphism: realized by virtual functions.
  • Static binding: the compilation process, if you know which function to call, as it is overloaded function to determine which function to call the parameter.
  • Dynamic binding: run-time binding, it is achieved by virtual functions.
  1. When you use a pointer or reference to access to non-virtual function, the compiler according to the type of the pointer itself decides which function to call. Rather than a pointer to the object than the type according to.
  2. When you use the pointer to access a virtual function, the compiler decides which function to call, regardless of the type of the pointer itself according to the type of pointer.

Derived class virtual function can be rewritten base class (redefinition, covered):

  1. And parameter types of the base class virtual function must be the same
  2. And the number of parameters of the base class virtual function must be the same
  3. And virtual functions of the base class must be the same as the return type (class type may be replaced by a sub pointer)

The default number of C ++ function call does not use dynamic binding, it must be met to trigger dynamic binding conditions:

  • Only designated as virtual functions can be dynamically bound
  • Must be called by a function pointer or reference to the base class

What functions can not be virtual

  1. Constructors: If the constructor can not call the parent class constructor properly
  2. Static member function: static member function only one code for each class, all objects share this code, which does not belong to a specific target at all.
  3. Friend function: simply not member functions, without mentioning the virtual function
  4. Restrained function:
  5. Assignment operator overloaded functions: different parameter types of the base class and subclass

The default C ++ destructor not virtual because the need for additional virtual function tables and virtual virtual function table pointer, additional memory. For not inherited class, its destructor If a virtual function, memory is wasted. Thus the default C ++ destructor not virtual, but only when it is necessary as a parent class virtual function set.

Virtual function table pointer (the vptr) and the virtual function table (bptr)

C ++ object model data members, member functions here:

  • Beyond all objects: static and non-static member functions, static data member is placed in the global static storage area.
  • Within the object: all non-static data members

virtual storage function of the following mechanisms:

  1. Each bunch of generating class virtual function pointer, in the table, this table is referred to as a virtual function table (bptr)
  2. Each object is a pointer to add virtual function table (vtable pointer vptr).

Different forms exist inheritance patterns object virtual function table: detail with reference to class content blog

  1. Single inheritance: only a derived class virtual function table. The virtual function table base class virtual function table and a table is not (whether there is a derived class does not override base class virtual function), but if derived class does not override base class virtual function, then, base and derived class virtual function function address table points are the same.
  2. Multiple inheritance: consistent sequence derived class virtual function table arrangement of the plurality, and inherited virtual functions. Derived class overrides a function of the same name will overwrite the contents of all vtable, the derived class virtual function new custom will later be expanded to the virtual function table of the first class .

Guess you like

Origin blog.csdn.net/saber_wtq/article/details/90691766