Application features of virtual functions)

Source: Internet
Author: User
Document directory
  • Application features of virtual functions
Application feature example of virtual functions: Write three classes: No virtual functions, a virtual function, and two virtual functions. Define the objects of these three classes in the main function, take the memory space occupied by this class and observe the memory allocation;
The Code is as follows: /**//*********************************** *************************************
* Application feature example of virtual functions
**************************************** ********************************/
# Include <iostream. h>
// Class without virtual functions
Class cnovirtual
... {Int X;
Public:
Cnovirtual (INT Nx)... {x = NX ;}
Void func () const ...{}
Int getx () const... {return X ;}
};

// A class with a virtual function
Class conevirtual
... {Int X;
Public:
Conevirtual (INT Nx)... {x = NX ;}
Virtual void func () const ...{}
Int getx ()... {return X ;}
};

// Classes with two virtual functions
Class ctwocirtuals
... {Int X;
Public:
Ctwocirtuals (INT Nx)... {x = NX ;}
Virtual void func () const ...{}
Virtual int getx () const... {return X ;}
};

Void main ()
... {Cnovirtual obj1 (100 );
Conevirtual obj2 (200 );
Ctwocirtuals obj3 (300 );
Cout <"size of types :";
Cout <"int:" <sizeof (INT) <Endl;
Cout <"cnovirtual:" <sizeof (cnovirtual) <Endl;
Cout <"Void *:" <sizeof (void *) <Endl;
Cout <"conevirtual:" <sizeof (conevirtual) <Endl;
Cout <"ctwovirtual:" <sizeof (ctwocirtuals) <Endl;
} 1. running result: 2. memory Allocation of objects: 3. object address and content: 4. address and content of the member variable of the object: In summary, the memory space of the object is allocated as follows: Description:
From the above results, we can see that the size of the class cnovirtual without virtual functions is exactly the size of its member variable-the size of an integer data, the size of the class with one virtual function and two virtual functions must be added with a compact NULL pointer type. This indicates that some information indicating the type is automatically added to the compilation system in the class containing virtual functions.
When a class has virtual functions, the compilation system creates an array vtable for the class. The element in the vtable array is the address of the virtual function, and the address of the same virtual function is the same as the offset at the first position in the vtable of the base class and the derived class. At the same time, the compilation system also adds the code for calling virtual functions. All these are jobs that do not need to be done by programmers and are automatically completed by the system. When initializing this type of object, a pointer to vtable will be added. This pointer is generally called vptr. Generally, vptr is located at the beginning of the storage unit of the class object, as shown in.
In this way, after the vptr is correctly initialized, it points to the vtable of the object and establishes a connection between the object and its specific virtual function definitions. In the sense of calling a virtual function, vptr indicates the type information, because it makes the call and the type match.

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.