c ++ virtual functions and pure virtual function, virtual inheritance

Role of virtual functions: c ++ achieve polymorphism, to ensure the unity of the function names; (virtual function through the parent class objects to sub-class pointer, in turn, can call a function in the subclass object rewritten)

Implement virtual functions: virtual keyword; in the parent class's virtual function marked is the virtual function; virtual function to achieve when succession to rewrite (ie subclass realize their own function);

Virtual function table: generated by the system itself; virtual function table content also inherited by the subclass; (vtable content may be rewritten and coverage);

Pure virtual function: virtual void f1 () = 0 class containing pure virtual function is a pure virtual class; not generate the object; 

Virtual base class: to avoid the problem of diamond inheritance (only copy of memory, two of the opposite sex without problems) inherited by virtual declaration;

virtual virtual function, the compiler stage, class more (entry address virtual table, stored virtual function) a vfptr pointer to vftable If the function in the base class virtual function, the derived function class with the same name with reference also virtual function ( The system automatically added)

 

 

RTTI (runtime type information): runtime type identification can be made by the pointer or reference to the base class pointer or reference check these incoming messages to determine the actual referent derived type, i.e. the runtime object type

Which contains two main functions typeid and functions of dynamic_cast Typeid tell the user what the current example is the type: int a = 0; typeid ( a) .name (); == "int

Dynamic: Allow the runtime type conversion, (the pointer or reference to the base class to derived class converted pointers or references);

/ * Specific implementation (abbreviated to understand you can) * /

When there is a virtual function in the class, the compiler will add a link to the virtual function table pointer vptr class member variable, type_info object associated with each class is also assigned out via a virtual table, typically in the type_info object the first slot table. When we dynamic_cast, compiler syntax checking will help us. If the same static type and the destination pointer type, then do nothing; otherwise, the first pointer is adjusted so that it points vftable, and the pointer after its adjustment, offset adjustment, the static type and target type is passed to an internal function. The last parameter indicates a pointer or reference conversion. The only difference between the two is that if the conversion failed, former returns NULL, which bad_cast throw an exception. 

 

Virtual functions become conditions: can take the address, call dependent objects;

For example: the object constructor call does not depend on (x) destructor (v) inline-linking can not take the address (x) static (x)

NOTE: To a certain destructor base class virtual function written (derived class implementation object's destructor) using a base pointer or reference or pointer to the derived class object reference

Pure virtual function: abstract class, can not instantiate objects,

Such as: Virtual void bark () = 0; // unimplemented

// do to use a pointer or reference (without generating object)

Virtual virtual inheritance (for example, this may repeat the question of succession conflict and wasted too much memory in the presence of diamond inheritance)

Vbptr, pointing to the base class indirectly, to hold offset (last offset scope) can be seen according to the implementation of virtual inheritance

 

Guess you like

Origin www.cnblogs.com/xcb-1024day/p/11269457.html