Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of virtual functions in C++?

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what is the use of virtual functions in C++", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn "what is the use of virtual functions in C++" this article.

Object-oriented programming languages have three main characteristics: encapsulation, inheritance, and polymorphism. C++ is an object-oriented language (the main difference from C), so C++ also has polymorphic features.

Polymorphism in C++ is divided into two types: static polymorphism and dynamic polymorphism.

Static polymorphism is when the compiler determines to call a function based on information such as the function name and parameters during compilation. Static polymorphism is mainly reflected in function overloading and operator overloading.

Function overloading means that multiple member functions with the same name are defined in the class, and the parameter types, number of parameters, and return values of the functions are not exactly the same. After compilation by the compiler, the function names of these functions with the same name will be different, that is to say, during compilation, it is determined to call a certain function. C language function compiled function name is the original function name, C++ function name is the original function name splicing function parameters and other information.

Dynamic polymorphism is runtime polymorphism, which determines the actual type of the referenced object during program execution (non-compilation period) and calls the corresponding method according to its actual type. Dynamic polymorphism is implemented by virtual functions.

such as

class Base{};class A: public Base{};class A: public Base{};Base *base = new A; //baseStatic type is Base*, dynamic type is A*base = new B; //baseDynamic type becomes B* Explore virtual function table structure

As mentioned in the previous document, the space occupied by a class, if there is a virtual function, will take up 8 bytes of space to store the address of the virtual function table.

Virtual function table memory space in turn stored in each virtual function pointer, through this pointer can call the relevant virtual function.

The following code to verify the above memory structure, define a Base class, there are three methods in the middle, f1/f2/f3.

class Base {public: virtual void f1(){ std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report