Implementation mechanism of C + + late bundle

Source: Internet
Author: User

Early binding (early binding) is defined by defining its properties and methods before instantiating the object, so that the compiler or interpreter can convert the machine code ahead of time.

late binding (late binding) refers to the type of object that the compiler or interpreter does not know before it runs. With late binding, you do not need to check the object's type, just check whether the object supports properties and methods.

The advantages of early binding are:
(1) High compilation efficiency
(2) a code hint
(3) Compile-time type checking
the advantages of late binding are:
(1) No declaration type
(2) object type can be changed at any time


The virtual keyword tells the compiler to implement a late bundle (virtual function).

To implement a late bundle, a typical compiler creates a table (VTABLE) for each class that contains virtual functions, and a virtual function address for a particular class is placed in VTABLE. In each class with a virtual function, the compiler places a pointer vptr, pointing to the vtable of the object. When a virtual function call is made through a base-class pointer, the compiler's static insertion can take the vptr and find the code for the function address in the Vtavle table, which causes the late bundle to occur.


 #define   _crt_secure_no_warnings#include<iostream>using  namespace std;class novirtual{public:void fun () {}int ret () {}private:int a;}; Class onevirtual{public:virtual void fun () {}int ret () {}private:int a;}; Class twovirtual{public:virtual void fun () {}virtual int ret () {}private:int a;}; Void test () {cout <<  "sizeof (int):"  << sizeof (int)  << endl ;cout <<  "sizeof (novirtual):"  << sizeof (novirtual)  << endl; cout <<  "sizeof (onevirtual):"  << sizeof (onevirtual)  << endl; cout <<  "sizeof (twovirtual):"  << sizeof (twovirtual)  << endl;} Int main () {test (); System ("pause"); return 0;} 

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/7C/wKiom1dCyIfwp_qkAAAKL7TeLs0904.png "title=" Capture 1. PNG "alt=" Wkiom1dcyifwp_qkaaakl7tels0904.png "/>

Since the size of class Onevirtual and class twovirtual has not changed, the above conclusions can be introduced.

This article is from the "Fun" blog, make sure to keep this source http://10725723.blog.51cto.com/10715723/1782205

Implementation mechanism of C + + late bundle

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.