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 memory structure of virtual functions in C++?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the memory structure of virtual functions in C++ is like, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!

Memory structure of virtual function

I. basic concepts

The function of virtual function in C++ is mainly to realize the mechanism of polymorphism.

With regard to polymorphism, in short, you use a pointer of the parent type to point to an instance of its subclass, and then call the member function of the actual subclass through the pointer of the parent class. This technique allows the pointer of the parent class to have "multiple forms", which is a generic technique.

The so-called generic technology, to put it bluntly, is to try to use immutable code to implement variable algorithms. For example: template technology, RTTI technology, virtual function technology, either try to make a decision at compile time, or try to do a run-time resolution.

2. the definition of virtual function and the redefinition in derived class

Class class name

{

Public:

Description of virtual member function

}

Class class name: access modifier base class name

{

Public:

Description of virtual member function

}

Third, the structure of virtual function in memory

1. Let's look at an example first.

# include

Using namespace std

Class A

{

Public:

Virtual void fun0 () {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