当前位置:网站首页>virtual function

virtual function

2022-07-23 11:58:00 Look, that's a licking dog

Virtual function is a specific form of object-oriented programming function , yes C++ An effective mechanism for implementing polymorphism .

polymorphic

C++ Polymorphism can be divided into static polymorphism and dynamic polymorphism . The polymorphism of function overload and operator overload is static polymorphism , Dynamic polymorphism can be achieved through virtual functions . The essential core of realizing the dynamic binding of functions is the virtual table pointer and virtual function table .

Virtual functions

When a pointer to a base class operates on its polymorphic class object , The corresponding functions will be called according to different class objects , This function is a virtual function , Virtual functions use virtual Decorated function name . The function of virtual function is to dynamically select the appropriate member function in the running stage of the program . Functions redefined in derived classes should have the same number and type of formal parameters as virtual functions ,( The order of parameter types should also be consistent ), To achieve a unified interface . If the virtual function is not redefined in the derived class , Then it inherits the virtual function of the base class .

Be careful
(1) Just use the keyword in the body of the class that declares the function virtual take Function declaration As the virtual function , Not required when defining functions
(2) After declaring a member function in the base class as a virtual function , A function with the same name in a derived class automatically becomes a virtual function
(3) If a class ( Base classes and derived classes ) A member function is declared as a virtual function , Then the same non virtual function can no longer appear in the class
(4) A non class member function cannot be defined as a virtual function , Global functions and static member functions of classes ( Because the static member function of the calling class does not need an instance , But calling a virtual function requires an instance ) And constructor ( Because the constructor runs before the object is completely constructed , Constructor is to initialize virtual table pointer , When you want to call a virtual function, you need to know the virtual table pointer , Contradictions exist , But virtual functions can be called in constructors )、 Inline functions cannot be defined as virtual functions , But generally, destructors are defined as virtual functions ( If the destructor is not defined as a virtual function , When using base class pointer delete when , Cannot call the destructor of a derived class , The derived class part cannot be destructed )
(5) Pointer declaration does not call constructor

When the compiler finds a virtual function in a class , A virtual function table is immediately generated for this class , The entries in the virtual function table are pointers to the corresponding virtual functions . The compiler also implicitly inserts a pointer into this class vptr Point to virtual function table . In addition, when calling the constructor of a class , The pointer to the base class has now become a pointer to a specific class this The pointer , It depends this You can get the right table, Only in this way can we really connect with the function body , This is dynamic linking , The basic principle of implementing polymorphism .

C++ In the inheritance 、 Virtual functions 、 What do pure virtual functions mean ?

Inherit : stay C++ The usage of inheritance in is as follows :class Derived class name :public/protected/private Base class name {}. It refers to that one thing automatically obtains all the things of another thing ( attribute , Ability ).

Virtual functions : use virtual A modified function is a virtual function , It implements class polymorphism . Call virtual functions as base class objects , If the object is a derived class , The corresponding function of the derived class will be called , Thus, we can make different types of objects make their own different responses through the same call form .

Pure virtual function : It's a special kind of virtual function

Is it possible to declare every function as a virtual function ?

Although virtual functions are very effective , But you can't declare every function as a virtual function . Because using virtual functions costs memory , Each virtual function object must maintain a virtual function table in memory , Extra overhead .

原网站

版权声明
本文为[Look, that's a licking dog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230538491536.html