Discussion on dynamic binding and static binding of C + +

Source: Internet
Author: User

In the previous study of C + +, the understanding of dynamic binding and static binding is: static binding is determined at compile time, non-virtual function is basically static binding, and dynamic binding is used for virtual function to realize polymorphism. This is not a big problem to understand, but I have always wondered, since static binding can be compiled at the time to think, why dynamic binding can not?

This article is well written: http://blog.csdn.net/chgaowei/article/details/6427731

I know the two concepts of static type and dynamic type (ashamed):

1. Static type of object: the type that the object takes when declaring. is determined at compile time.

2. Dynamic type of object: The type of object currently being referred to. is determined at run time. The dynamic type of the object can be changed, but the static type cannot be changed.

My understanding is that the static type is the type of the object pointer, such as declaring base B, and the type of variable B is base. The dynamic type is the type of object that the pointer points to, such as Derived d = new Derived (), Base B = &d, and the type that the pointer points to is d. The dynamic type of B is changing with the execution of the program, so it needs to be decided at runtime.

These two concepts can be reminiscent of a pointer-related concept in C:

int a = ten; int* p = &a;

Here, the pointer type is int*, and the type that points to is int, so the understanding is clear.

Of course, this can be "dynamically binding" a bit:

int* q = (char*) p;

The type of Q here is still int*, but the point of the type is char, and the print is char, because the size end stores the truncation reason.

Discussion on dynamic binding and static binding of C + +

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.