Popular analysis of early binding and late binding techniques

Source: Internet
Author: User

Concept:

Early binding refers to binding in the compile phase

Late binding refers to run phase binding


Example:

Class Base

{

void Fun () {}

}

Class Bind:class Base

{

Void fun () {}

}

Main ()

{

Base base;

Base->fun ();

Bind *tmp;

TMP = &base;

Tmp->fun ();

}


The above example shows you whether there is a grammatical problem, in fact, I deliberately, the fun function is covered

First, say Base->fun (); it's a process.


A base object was created when the program was run, Base->fun (); What is it?

If you understand the object to call the function in the object, then it becomes the runtime to find the function, that good early binding it?

C + + compile, base->fun () is actually equivalent to fun (base), so a look is not the function address! is indeed a function address,

This also indirectly indicates that the member function in C + + is a unique global address, and the object is a hidden argument.


Early binding refers to the decision in the compilation phase of how to call, compiled to determine the call address, of course, determine the way to call, so that is early binding.

Why does Tmp->fun invoke Bind's fun function because C + + uniquely determines the globally unique function by the function name + variable type + number of variables.

This is why the C + + function call design is not the same as the reason for it!


In fact, when you are understanding the early binding technology, I think I can also handle late binding technology.

Late binding is used to solve polymorphic properties (polymorphic properties are not explained here)

Late binding means that even if you assign a value, you can correctly invoke the correct function.

The solution for C + + is:

When a function is called and a special tag (virtual) is checked, the address is not called directly, and a vptr is taken out of the base object.

vptr points to the virtual table so that the function can be correctly found to be called. Which is to wrap around and,

Of course, it is easy to say, it is difficult to achieve such a function.


Alternative solutions:

The virtual function is saved directly to the object, and when the special tag (vitrual) is checked, the function address is called directly.

When I think the plan is immature. Just to show that late binding is the technology we often use, and

Not mysterious, special, complex.

Popular analysis of early binding and late binding techniques

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.