"Summary" about C + + virtual functions, polymorphism, and object cutting

Source: Internet
Author: User

Seemingly few of these things connected together, in the online search for a long time, also turned a half-day book to understand how the whole process is going on.

First of all, the principle of phenomenon:

1 //VtableTest.cpp:Defines the entry point for the console application.2 //3 4#include"stdafx.h"5#include"iostream"6 using namespacestd;7  8 classBase9 {Ten  Public:  One     Virtual voidFunction1 () {cout <<"Base F1"<<Endl;}; A     Virtual voidFunction2 () {cout <<"Base F2"<<Endl;}; - }; -  the classD1: PublicBase - {  -  Public: -     Virtual voidFunction1 () {cout <<"D1 F1"<<Endl;}; + };  -  + int_tmain (intARGC, _tchar*argv[]) A {   atbase* pb0 =NewBase (); -Pb0->function1 (); -  -base* PB =NewD1 (); -Pb->function1 (); -Pb->function2 (); inPb->Base::function1 (); -  to D1 D1; +Base B =D1; - B.function1 (); the B.function2 (); * GetChar ();  $     Deletepb0;Panax Notoginseng     DeletePB; -     return 0; the}

This output is:

Base F1
D1 F1
Base F2
Base F1
Base F1
Base F2

virtual function:

When virtual functions are defined in class, the compiler adds a virtual pointer (vptr) to the class and generates a virtual function table (vtable).

1 classBase2 {3  Public: 4     Virtual voidFunction1 () {cout <<"Base F1"<<Endl;};5     Virtual voidFunction2 () {cout <<"Base F2"<<Endl;};6 };7 8 classD1: PublicBase9 { Ten  Public: One     Virtual voidFunction1 () {cout <<"D1 F1"<<Endl;}; A};

The base in the example has a table of its own virtual functions, with function1 and function2 two functions in the table. When the call

    New Base ();    PB->function1 ();

This will find the object's location in memory based on PB, then find the vptr generated by the compiler, find the vtable based on Vptr, and execute the Function1 method inside.

The output at this time is "Base F1".

Polymorphic:

And when it's time to play:

    New D1 ();    PB->function1 ();

Since D1 inherits base, it inherits the vptr of base, but generates a separate vtable that belongs to D1. Vptr will point to this new D1 vtable.

The Function1 method is overridden in the definition of D1, that is, in D1 vtable, Function1 is overwritten by the new method (the one defined in D1), and function2 is the same as in base.

So when calling Function1 (), the object will be located in memory based on PB, and then find the object generated by the compiler vptr (this vptr is vtable to D1), vtable () in the execution function1, This function1 () is the one defined in D1.

So the output is "D1 F1" at this time.

Of course, if you want to execute the virtual function of the parent class is also possible, after all, the base vtable is still there, the direct call is OK.

Pb->base::function1 ();

Add Base:: Will indicate (virtual? ) function table, let the system directly to find the corresponding function to execute.

This output is still "Base F1".

Object cutting:

It's not funny at all.

    D1 d1;     = D1;    B.function1 ();

The memory occupied by D1 is made up of two parts: his parent class base and D1 's own unique content. When performing base b = d1; Object slicing occurs when B has only a member of the base class, and the members of the D1 are all discarded.

But the members of B should be copied from the D1, but for Mao Function1 will output "Base F1" Ah This problem troubled me for a long time.

Base B = d1; The process is slightly more complicated.

Because there are virtual functions in base, it is not possible to "copy by Bit" (Bitwise copy). The compiler will generate a separate assignment function for base and reset Vptr to point to Base's own vtable.

This is done in order to prevent the virtual function from calling the members of the subclass, so that the parent class is cut off to call the subclass of the method program should be hung out.

So this example will still output "Base F1".

Reference:

1, "in-depth exploration of C + + object Model"

2,http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/(This is more detailed)

"Summary" about C + + virtual functions, polymorphism, and object cutting

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.