In-depth understanding of dynamic and static binding of C ++

Source: Internet
Author: User

Dynamic binding and static binding are used only to support C ++ polymorphism. Understanding their differences helps to better understand polymorphism and avoid mistakes during programming.
Four terms need to be understood:
1. static object type: the type of the object to be declared. It is determined during the compilation period.
2. Dynamic Object type: the type of the object currently referred. It is determined during the runtime. The dynamic type of the object can be changed, but the static type cannot be changed.
For more information about the static and dynamic types of objects, see the following example:

View plaincopy to clipboardprint?

  1. Class B
  2. {
  3. }
  4. Class C: Public B
  5. {
  6. }
  7. Class D: Public B
  8. {
  9. }
  10. D * Pd = new D (); // The static type of PD is its declared type D *, and the dynamic type is also D *
  11. B * pb = Pd; // The static type of Pb is its declared type B *, and the dynamic type is the type of the object PD pointed to by PB *
  12. C * Pc = new C ();
  13. PB = pc; // The dynamic type of Pb can be changed, and now its dynamic type is C *

3. Static binding: the static type of the object is bound. A feature (such as a function) depends on the static type of the object and occurs during compilation.
4. dynamic binding: the dynamic type of the object is bound. A feature (such as a function) depends on the dynamic type of the object and occurs at runtime.

View plaincopy to clipboardprint?

  1. Class B
  2. {
  3. Void dosomething ();
  4. Virtual void vfun ();
  5. }
  6. Class C: Public B
  7. {
  8. Void dosomething (); // first, this subclass redefines the no-virtual function of the parent class. This is a bad design and causes name masking; this is only used to describe dynamic binding and static binding.
  9. Virtual void vfun ();
  10. }
  11. Class D: Public B
  12. {
  13. Void dosomething ();
  14. Virtual void vfun ();
  15. }
  16. D * Pd = new D ();
  17. B * pb = Pd;

Let's take a look. Do PD> dosomething () and Pb> dosomething () call the same function?
No, although both PD and Pb point to the same object. Because the function dosomething is a no-virtual function, it is statically bound, that is, the compiler selects the function according to the static type of the object during compilation. If the static type of PD is D *, the compiler points to D: dosomething () when processing Pd-> dosomething (). Similarly, if the static type of Pb is B *, Then Pb-> dosomething () calls B: dosomething ().

Let's take a look. Are Pd-> vfun () and Pb-> vfun () Calling the same function?
Yes. Because vfun is a virtual function, it is dynamically bound, that is to say, it is bound to the dynamic type of the object. Although Pb and PD have different static types, they point to an object at the same time, their dynamic types are the same, all of which are D *. Therefore, they call the same function: D: vfun ().

The above are all for object pointers, which are also applicable to reference cases.

The dynamic and static types of the pointer and reference may be inconsistent, but the dynamic and static types of the object are consistent.
D;
D. dosomething () and D. vfun () always call D: dosomething () and D: vfun ().

For those dynamic binding and static binding, I have summarized the following articles:
I summarized one sentence: only virtual functions use dynamic binding, and all others are static binding. At present, I have not found that this sentence is not applicable. If there is an error, I hope you can point it out.

Special notes
When the default parameters and virtual functions appear together, the situation is a little complicated and prone to errors. We know that virtual functions are dynamically bound, but the default parameter is static for execution efficiency.

View plaincopy to clipboardprint?

  1. Class B
  2. {
  3. Virtual void vfun (INT I = 10 );
  4. }
  5. Class D: Public B
  6. {
  7. Virtual void vfun (INT I = 20 );
  8. }
  9. D * Pd = new D ();
  10. B * pb = Pd;
  11. Pd-> vfun ();
  12. Pb-> vfun ();

The above analysis shows that Pd-> vfun () and Pb-> vfun () are called Functions d: vfun (), but what are their default parameters?
For analysis, the default parameter is statically bound. When Pd-> vfun () is used, the static type of PD is D *, so the default parameter is 20. Similarly, pb-> the default value of vfun () is 10. The code is correct.
No one will like this feature. So always remember:
"Never redefine the inherited default parameter (never redefine function's inherited default parameters value .)"

About C ++
At present, I basically work under the "Object-Oriented Programming" subset of C ++, but I do not know much about more complex knowledge. Even so, there have been a lot of things to pay attention to during programming so far, and it may continue to increase later. This may be the reason why many people oppose C ++.
C ++ is one of Google's four official languages. However, Google has launched the go language in recent years, and its positioning is similar to that of C/C ++. Considering this situation, I think it may be that Google programmers are deeply aware of the complexity of C ++, so they want to develop a C ++ alternative language. You have time to understand the go language and how to choose it when it is similar to 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.