(c + +) static and dynamic binding in C + + polymorphism

Source: Internet
Author: User

static binding and dynamic binding are a feature of C + + polymorphism.

1. Static type and dynamic type of object:
    • Static type of object:

The object is in the type of declaration, determined at compile time;

    • The dynamic type of the object:

The type that the current object refers to, at run time, determines that the dynamic type of the object can be changed, but the static type cannot be changed.

Class B{};class C:public b{};class d:public b{};D * Pd=new D ();//static type of PD is its declared type d*, dynamic type is also d*b* pb=pd;//PB static type is its declared type b*, dynamic The state type is the type of the object PD referred to by PB d*c* pc=new C ();//The static type of the PC is the type it declares c*, the dynamic type is also c*pb=pc;//PB dynamic type can be changed, and now its dynamic type is c*
2. Static binding and dynamic binding
    • Static bindings:

The binding is the static type of the object, and an attribute (such as a function) depends on the static type of the object, which occurs at compile time.

    • Dynamic binding:

The binding is the dynamic type of the object, and an attribute (such as a function) depends on the dynamic type of the object, which occurs at run time.

Class b{    void DoSomething ();    virtual void Vfun ();}; Class C:public b{    ///First of all, this subclass redefined the no-virtual function of the parent class, which is a bad design that causes the name to be obscured; this is just to illustrate the use of dynamic and static bindings.      void DoSomething ();    virtual void Vfun ();}; Class D:public b{    void dosomething ();    virtual void Vfun ();;D * Pd=new D ();//The static type of PD is its declared type d*, the dynamic type is also d*b* pb=pd;//PB's static type is its declared type b*, and the dynamic type is the type of the object PD referred to by PB d*

is the same function called by pd->dosomething () and pb->dosomething ()?

Answer: No, although PD and PB point to the same object, but the function dosomething is a non-virtual function, it is statically bound, that is, the compiler will select the function according to the static type of the object, the static type of PD is d*, then the compiler handles pd- >dosomething () points to D::D osomething (). Similarly, the static type of PB is b*, then pb->dosomething () is called B::D ossomething ().

Are the same functions called by Pd->vfun () and Pb->vfun ()?

Answer: Yes, Vfun is a virtual function, it is dynamically bound, that is, it is bound to the dynamic type of the object, PB and PD although static types are different, but they point to an object, their dynamic objects are the same, are d*, so, they call the same function: D::vfun ().

All of this is for the object pointer, which is also true for references (reference).

The dynamic and static types of pointers and references may be inconsistent, but the object's dynamic and static types are consistent.
e e;
D.dosomething () and D.vfun () are always called D::D osomething () and D::vfun ().

Summary: Only virtual functions use dynamic bindings, and all others are static bindings.

when the default parameters and virtual functions appear together, the situation is a bit complicated and prone to error. We know that virtual functions are dynamically bound, but for efficiency, the default parameters are statically bound .

Class b{    void DoSomething ();    virtual void vfun (int i=10);}; Class D:public b{    void dosomething ();    virtual void vfun (int i=20);};D * Pd=new D ();//The static type of PD is its declared type d*, the dynamic type is also d*b* pb=pd;//PB's static type is its declared type b*, the dynamic type is PB refers to the object of the PD type D*pd->vfun ();p B->vfun ( );

With the above analysis, the Pd->vfun () and Pb->vfun () calls are function D::vfun (), but what are their default parameters?

Analysis, the default parameter is statically bound, pd->vfun (), the static type of PD is d*, so its default parameter is 20, and the static type of PB is b*, so the default parameter of Pb->vfun () is 10.

For this feature, it is estimated that no one will like it. So, always remember:
"Never redefine inherited default parameters (never redefine function ' s inherited default parameters value.)"

Reference article:

http://blog.csdn.net/chgaowei/article/details/6427731

(c + +) static and dynamic binding in C + + polymorphism

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.