Deep understanding of the application of C + + dynamic binding and static binding _c language

Source: Internet
Author: User
In order to support the polymorphism of C + +, dynamic binding and static binding are used. Understanding their differences helps to better understand polymorphism and avoid making mistakes during programming.
need to understand four nouns:
1. Static type of object: the type to be used when the object is declared. is determined at compile time.
2. The dynamic type of the 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 the static and dynamic types of objects, see an example:
Copy Code code as follows:

Class B
{
}
Class C:public B
{
}
Class D:public B
{
}
d* PD = new D ();//pd's static type is the type d* it declares, and the dynamic type is also d*
b* PB = PD;//PB's static type is the type b* it declares, and the dynamic type is the type of the object PD the PB points to d*
c* PC = new C ();
PB = PC;//PB dynamic type 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: Binding is the dynamic type of the object, an attribute (such as function) depends on the dynamic type of the object, occurs in the runtime.
Copy Code code as follows:

Class B
{
void DoSomething ();
virtual void Vfun ();
}
Class C:public B
{
void DoSomething ()//First of all, this subclass defines the No-virtual function of the parent class, which is a bad design that leads to name concealment; This is only used to illustrate dynamic binding and static binding.
virtual void Vfun ();
}
Class D:public B
{
void DoSomething ();
virtual void Vfun ();
}
d* PD = new D ();
b* PB = PD;

Let's see, pd->dosomething () and pb->dosomething () call the same function?
No, although PD and PB all point to the same object. Because the function dosomething is a no-virtual function, it is statically bound, that is, the compiler will select the function based on the static type of the object at compile time. The static type of PD is d*, and the compiler points it to D::D osomething () when processing pd->dosomething (). Similarly, PB's static type is b*, and that pb->dosomething () is called B::D osomething ().
Let's take a second look, Pd->vfun () and Pb->vfun () call the same function?
Yes. Because Vfun is a virtual function, it is dynamically bound, that is, it binds to the dynamic type of the object, PB and PD, although static types are different, but they point to an object at the same time, their dynamic type is the same, are d*, so they call the same function: D::vfun ().
The above are all cases of object pointers, which are also true for references (reference).
The dynamic and static types of pointers and references may be inconsistent, but the dynamic and static types of the objects are consistent.
D D;
D.dosomething () and D.vfun () are always called D::D osomething () and D::vfun ().
As for those things that are dynamically bound, those things are statically bound, there is an article that sums up very well:
I'm summed up the sentence: only virtual functions to use dynamic binding, the rest is all static binding. At present I have not found that does not apply to this sentence, if there is a mistake, I hope you can point out.
places that need special attention.
When the default and virtual functions appear together, the situation is a bit complicated and prone to error. We know that virtual functions are dynamically bound, but in order to perform efficiently, the default parameters are statically bound.
Copy Code code as follows:

Class B
{
virtual void Vfun (int i = 10);
}
Class D:public B
{
virtual void Vfun (int i = 20);
}
d* PD = new D ();
b* PB = PD;
Pd->vfun ();
Pb->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, when Pd->vfun (), the static type of PD is d*, so its default parameter should be 20; Similarly, the default argument for Pb->vfun () should be 10. Write the code to verify the 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
At the moment 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 programming so far, and it may continue to grow later, which is probably why many people oppose C + +.
C + + is one of the four official languages of Google. But Google has launched the go language in recent years, and the location is similar to C + +. Considering this situation, I think that Google's software developers may be deeply involved in C + +, so they want to develop an alternative language for C + +. Have time to get to the go language and see how it's going to be when it comes to problems like 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.