In depth understanding of C++ dynamic binding and static binding application details

  • 2020-04-01 23:38:18
  • OfStack

Dynamic binding and static binding are used to support polymorphism in c++. Understanding their differences can help you better understand polymorphism and avoid mistakes in programming.
There are four nouns to understand:
1. Static type of the object: the type of the object when it is declared. This is determined at compile time.
2. Dynamic type of the object: the type of the object currently referred to. It's decided at run time. The dynamic type of the object can be changed, but the static type cannot.
For static and dynamic types of objects, see an example:

class B
{
}
class C : public B
{
}
class D : public B
{
}
D* pD = new D();//The static type of pD is its declared type, D*, and the dynamic type is also D*
B* pB = pD;//The static type of pB is the type B* it declares, and the dynamic type is the type D* of the object pD that pB points to
C* pC = new C();
pB = pC;//PB's dynamic type can be changed, and its dynamic type is now C*

3, static binding: binding is the static type of the object, a feature (such as a function) depends on the static type of the object, occurs at compile time.
4, dynamic binding: binding is the dynamic type of the object, a feature (such as a function) depends on the dynamic type of the object, occurs at run time.

class B
{
    void DoSomething();
    virtual void vfun();
}
class C : public B
{
    void DoSomething();//First of all, this subclass redefines the parent class's no-virtual function, which is a bad design and causes name masking. This is only used to illustrate dynamic 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 > Does DoSomething() call the same function?
No, although pD and pB both point to the same object. Because the function DoSomething is a no-virtual function, it is statically bound, meaning that the compiler selects the function at compile time based on the static type of the object. The static type of pD is D*, so the compiler is dealing with pD- > DoSomething() points it to D::DoSomething(). Similarly, pB star is statically typed as B star, so pB star minus > DoSomething() calls B::DoSomething().
So let's see, pD- > Vfun (), and pB - > Is vfun() calling the same function?
B: 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 are static types are different, but they both point to an object, their dynamic type is the same, are D*, so, they call the same function: D::vfun().
All of the above is for object Pointers, and the same is true for references.
The dynamic and static types of Pointers and references may differ, but the dynamic and static types of objects are the same.
D D;
D. oSomething() and d. fun() always invoke D::DoSomething() and D::vfun().
As for those things that bind dynamically and those things that bind statically, this article sums it up nicely:
I've concluded that only virtual functions use dynamic bindings, and everything else is static. At present, I have not found anything that does not apply to this sentence. If there is any mistake, I hope you can point it out.
Special attention is needed
When default parameters and virtual functions appear together, the situation is a bit complicated and error prone. We know that virtual functions are dynamically bound, but the default parameters are statically bound for efficiency.

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();

We know from the above analysis that pD- > Vfun (), and pB - > Vfun () calls are functions D::vfun(), but what are their default parameters?
To analyze, the default parameter is statically bound, pD- > When vfun(), pD is statically typed D*, so its default parameter should be 20; By the same token, the pB - > The default parameter for vfun() should be 10. Write the code to verify that it is correct.
No one is likely to like this feature. So, always remember:
"Never redefine inherited default parameters (Never redefine the function 's inherited the default parameters value."
About c++
So far I've mostly worked with a subset of c++ called object oriented programming, and I don't know much about the more complex stuff. Even so, there's a lot to watch out for in programming so far, and there's likely to be more to come, which is probably why so many people are against c++.
C++ is one of Google's four official languages. However, Google has released go in recent years, and it is positioned to be similar to c/c++. In this case, I think it might be that the programmers at Google were so impressed by the complexity of c++ that they wanted to develop an alternative language. There is time to learn about the go language and see how it is balanced on issues like c++.

Related articles: