当前位置:网站首页>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 .
边栏推荐
猜你喜欢

3. DQL (data query statement)

2、MySQL数据管理--DML(添加、修改、删除数据)

Mosaic the face part of the picture

Data warehouse 4.0 notes - user behavior data collection III
![[pyrodiomics] the extracted image omics characteristic value is abnormal (many 0 and 1)](/img/73/628e0188e1e2c13e0963746759f691.png)
[pyrodiomics] the extracted image omics characteristic value is abnormal (many 0 and 1)

数仓4.0笔记---用户行为数据生成

11、多线程

Activiti7 Quick Start experience sharing

飞桨高层API实现图像去雨
![[system problems] Net Framework 3.5 installation error](/img/a1/f5410e954b607d5c0549051bd68aa1.png)
[system problems] Net Framework 3.5 installation error
随机推荐
paddle.nn.BCELoss的使用问题
Installation and process creation of activiti app used by activiti workflow
Ten year structure five year Life-02 first job
八、集合框架和泛型
UE4.24版本VR项目打包后,未出现手柄控制器
Introduction to the process structure used by activiti workflow
Ffmpeg audio coding
Es operation command
NFT digital collection system development: Shenzhen Evening News "good times travel" digital collection online seconds chime
MySQL storage engine
Internet communication
Scala II process control
Adding environment variables and templates to systemctl service
3、DQL(数据查询语句)
[untitled]
[literature research] search PubMed for papers in journals with specific impact factors
VMware uses wireless network card NAT to access the Internet under Windows
Standardize database design
1.认识数据库
方法的定义应用