Memory layout of classes in the single inheritance state where C ++ contains virtual functions

Source: Internet
Author: User
The following test code:


# Include <iostream>
Using namespace std;

Class base
...{
Public:
Base ()
...{
Cout <"create base" <endl;
};
Virtual ~ Base ()
...{
Cout <"clean base" <endl;
};
Virtual void foo_1 () = 0;
Virtual void foo_2 ();
Void foo_3 ();
};

// Implementation
Void base: foo_2 ()
...{
Cout <"virtual base: foo_2" <endl;
}
Void base: foo_3 ()
...{
Cout <"isnt virtual base: foo_3" <endl;
}
// Inheritance class
Class inherit: public base
...{
Public:

Inherit ()
...{
Cout <"create inherit" <endl;
};
~ Inherit ()
...{
Cout <"clean inherit" <endl;
};
// Virtual void foo_0 () = 0;
Virtual void foo_1 ();
Virtual void foo_2 ();
Void foo_3 ();
};
// Implementation

Void inherit: foo_1 ()
...{
Cout <"virtual inherit: foo_1" <endl;
}
Void inherit: foo_2 ()
...{
Cout <"virtual inherit: foo_2" <endl;
}

Void inherit: foo_3 ()
...{
Cout <"isnt virtual inherit: foo_3" <endl;
}
Int main ()
...{
Base * PTR = new inherit;

PTR-> foo_1 ();
PTR-> foo_2 ();
PTR-> foo_3 ();

Delete [] PTR;

Int nwait;
Cin> nwait;
Return 0;
}

Test in vc7:

Create base
Create inherit
Virtual inherit: foo_1
Virtual inherit: foo_2
Isnt virtual base: foo_3
Clean inherit
Clean Base

The following two conclusions can be drawn from the results:

(1) the virtual function that points to the base class pointer of the derived class is in the derived class;

(2) the class function (non-virtual, non-static) called by the base class pointer pointing to the derived class is the function of the original class pointer;

The layout of virtual and non-virtual functions in the class is different;

How can a pointer of a base class call a function of a non-derived class?

Cause:
Each class containing virtual functions has a virtual function table (VTB), which is generated during compilation.

Then, each class object has a pointer to the virtual table, and corresponding functions can be called in the virtual table.

Continue to analyze the example above,

Because

Base * PTR = new inherit;

The inherit object is generated. during the execution of the inherit constructor, The vtb creation process of inherit is completed, and a pointer pointing to the vtb is automatically generated in the created inherit.

The line surface will explain the above conclusions (1) and (2) respectively)

Because the ptr address points to the inherit object, the pointer type is still the base class.

When calling a virtual function, we first call the pointer to vtb. We call this pointer ptrVtb, and then obtain the relative value of vtb through the index value of this function.

Because the address of a class function (non-virtual function, non-static function) is not in the specific object of the class, the ptr is used to call the class function (non-virtual function, non-static function) the base class function is called!

Related Article

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.