To support the polymorphism of C + +, dynamic binding and static binding are used. Understanding their differences can help to better understand polymorphism and avoid making mistakes during programming.
There are four nouns to understand:
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.
For a static type and dynamic type of an object, see an example:
1 classB2 {3 }4 classE | PublicB5 {6 }7 classB | PublicB8 {9 }Tend* PD =NewD ();//the static type of PD is the type d* it declares, and the dynamic type is also d* Oneb* PB = PD;//the static type of PB is the type b* it declares, and the dynamic type is the type of the object PD that PB points to d* Ac* PC =NewC (); -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, and an attribute (such as a function) depends on the static type of the object, which occurs at compile time.
4. Dynamic binding: The dynamic type of the object is bound, and an attribute (such as a function) depends on the dynamic type of the object, which occurs at run time.
1 classB2 {3 voiddosomething ();4 Virtual voidVfun ();5 }6 classC: PublicB7 {8 voidDoSomething ();//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. 9 Virtual voidVfun ();Ten } One classB | PublicB A { - voiddosomething (); - Virtual voidVfun (); the } -d* PD =NewD (); -b* PB = PD;
Let's see if pd->dosomething () and pb->dosomething () are calling 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 chooses the function at compile time based on the static type of the object. The static type of PD is d*, and the compiler points to D::D osomething () when it Processes pd->dosomething (). Similarly, the static type of PB is b*, and that pb->dosomething () is called B::D osomething ().
Let's take a look again, is the same function called by Pd->vfun () and Pb->vfun ()?
Yes. Because Vfun is a virtual function, it is dynamically bound, that is, it binds to the object's dynamic type, PB and PD although static types are different, but they point to an object, their dynamic type is 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.
D D;
D.dosomething () and D.vfun () are always called D::D osomething () and D::vfun ().
As for those things the dynamic binding, those things static binding, has an article summarized very well:
I summed up one sentence: only virtual functions use dynamic binding, and all others are static bindings. At present I have not found that does not apply this sentence, if there are errors, I hope you can point out.
A particular area to note
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.
1 classB2 {3 Virtual voidVfun (inti =Ten);4 }5 classD: PublicB6 {7 Virtual voidVfun (inti = -);8 }9d* PD =NewD ();Tenb* PB =PD; OnePd->Vfun (); APb->vfun ();
The above analysis shows that 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 should be 20; Similarly, the default parameter of Pb->vfun () should be 10. Write the code to verify that it is correct.
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.)"
About the C + + language
I'm basically working on a subset of C + + "object-oriented programming," and I don't know much about more complex knowledge. Even so, there are a lot of things to be aware of in programming so far, and there may be a lot more to follow, which may be why many people oppose C + +.
C + + is one of Google's four official languages. But Google has launched the go language in recent years, and its positioning is similar to that of C + +. Considering this, I think it may be that Google's developers are deeply involved in C + +, so they want to develop an alternative language for C + +. Have time to get to know the go language and see how it chooses to do things like C + +.
In-depth understanding of C + + dynamic binding and static binding