C ++ virtual use and introduce the principle: the decision to postpone pointer

This blog has been studied for the students to think again and remember, scholars do not understand the basic programming language and compiler theory based unfriendly

virtual fundamental principle

Whether virtual function, or class hierarchy, his real function is to postpone the decision pointer,

The decision to postpone the decision at compile time to run, so there will be multi-state implementation of the principle of polymorphism is dynamic binding at runtime.

Virtual base class inherit the class variable is a pointer may point to defer, solves the ambiguity problem subclass and storing waste problem

Compile: function name sign pointing virtual functions are empty

Run: virtual function pointer pointing object domain, not class field

What function can be virtual

General function may be a member
constructors can not be virtual
destructor may be a virtual function
general virtual function member
declaration: virtual return_type func_name (formal_parameter_list)
declarations are only function prototype declaration in the class definition of
the derived class can yl class member functions cover (do not rewrite inherited non-virtual function, not because of the effect of polymorphism)
virtual functions are generally not declared inline function, because the call to the virtual function is dynamic, and within inline function is processed compile static
in addition, 1 when derived class to override virtual functions to cover the base class, will be defined as a virtual function, whether the addition of the virtual keyword whether function declaration

2. Other functions of the same name overloading derived class virtual function overloading the base class's display will be hidden (temporarily not sure why)

Therefore, when the cover is generally derived class virtual function group functions, plus how virtual keyword general, improve readability

Virtual destructor

Usage scenarios: pointer to the call object to allow others to use the base class (base class object or a derived class) destructor, we need to use a virtual destructor - this is to be done, delete operation will be normal; the contrary, if not virtual destructor, delete the behavior is unpredictable, since this time destructor base class object pointer points to an object pointer to a function table stored in the static binding to the base class destructor, when using multi-state, we when the base class derived object pointer operation polymorphic base class destructor does not release the memory space associated derived object, cause a memory leak.

Implement virtual keyword, dynamic binding to achieve glimpse view

Virtual table
for each polymorphic class has a virtual table (virtual table), the current virtual table has an entry address in each class virtual function, each object has a pointer to a virtual table of the current class (the vptr), based on multi-state virtual table, virtual keyword tells the compiler that period, some of which function entry address to put the virtual table, and determine the value of the virtual table, when you call the find function entry address table based on the value of the virtual runtime initialization run.

This article is part of notes and think about what C ++ Course address ZHENG Ying Tsinghua University teacher participation, tutorial if you say vague, you can go and see examples of courses and

Published 24 original articles · won praise 23 · views 10000 +

Guess you like

Origin blog.csdn.net/TowerOs/article/details/104089199