C ++ virtual function table and Memory Model

Source: Internet
Author: User

1. Virtual Functions

Virtual functions are a powerful weapon for implementing polymorphism in c ++. To declare virtual functions, you only need to add virtual keywords before the functions. virtual keywords are not required for the definition of virtual functions.

2. Highlights of virtual functions

(1)Static member functions cannot be declared as virtual functions.

It can be understood that a virtual function is a dynamic concept. During running, it determines which one to call, while a static function is a static concept. A more detailed explanation is that static functions are irrelevant to objects and cannot be called by object instances. This is obviously contrary to the concept of virtual functions.

(2)The constructor cannot be a virtual function.

Obviously, the creation of the virtual function table is carried out in the constructor. If the constructor is a virtual function, it becomes a problem of being a zombie.

(3)The Destructor is preferably a virtual function.

It is best to declare a virtual function unless you do not want to use it as a base class, in this way, the delete sub-class Object pointed to by the parent class pointer can automatically find the sub-class destructor.

3. Structure of the virtual function table in memory (gcc)

Virtual function tables are generally located at the very beginning of the memory occupied by objects.

(1) No inheritance

If there is no inheritance, it is meaningless to discuss the virtual function. However, it must be noted that the virtual function is placed in the declared order.

(2) General inheritance, no virtual function coverage

In this case, the virtual function pointer of the parent class is put in the virtual function table, and then the virtual function pointer is placed in the declared order.

(3) single inheritance with virtual function coverage

The virtual function table of the subclass object overwrites the parent class's virtual function and places its own virtual function pointer)

(4) Multi-Inheritance

Multiple inheritance means that multiple virtual function tables exist ,.

This is actually a blog summary on the Internet. This blog is very well summarized, so I have extracted it.

Supplement:

4. Differences in virtual functions and virtual base memory layout in GCC and VC

Key points: there is no difference in normal inheritance. When a single inheritance is performed, the virtual function tables are shared. However, in virtual inheritance, virtual function pointers are not shared in the vc environment, this means that a new virtual function requires an Extra pointer.

5. An example of virtual functions, polymorphism, and memory Layout

6. concepts related to Polymorphism

It clearly describes the differences between Overloading, hiding, and overwriting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.