Home > php教程 > PHP开发 > In-depth understanding of the application of dynamic binding and static binding in C++

In-depth understanding of the application of dynamic binding and static binding in C++

高洛峰
Release: 2016-12-26 16:21:18
Original
1109 people have browsed it

In order to support the polymorphism of C++, dynamic binding and static binding are used. Understanding their differences can help you better understand polymorphism and avoid making mistakes during programming.
You need to understand four nouns:
1. The static type of the object: the type used by the object when it is declared. It is determined at compile time.
2. Dynamic type of object: the type of the object currently pointed to. It is determined during runtime. The dynamic type of an object can be changed, but the static type cannot be changed.
Regarding the static type and dynamic type of objects, look at an example:

class B
{
}
class C : public B
{
}
class D : public B
{
}
D* pD = new D();//pD的静态类型是它声明的类型D*,动态类型也是D*
B* pB = pD;//pB的静态类型是它声明的类型B*,动态类型是pB所指向的对象pD的类型D*
C* pC = new C();
pB = pC;//pB的动态类型是可以更改的,现在它的动态类型是C*
Copy after login

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

class B
{
    void DoSomething();
    virtual void vfun();
}
class C : public B
{
    void DoSomething();//首先说明一下,这个子类重新定义了父类的no-virtual函数,这是一个不好的设计,会导致名称遮掩;这里只是为了说明动态绑定和静态绑定才这样使用。
    virtual void vfun();
}
class D : public B
{
    void DoSomething();
    virtual void vfun();
}
D* pD = new D();
B* pB = pD;
Copy after login

Let's take a look, do pD->DoSomething() and pB->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, 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*, so the compiler will point it to D::DoSomething() when processing pD->DoSomething(). In the same way, the static type of pB is B*, then pB->DoSomething() calls B::DoSomething().
Let us take a look again, do pD->vfun() and pB->vfun() call 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, and their dynamic types are the same. is D*, so they call the same function: D::vfun().
The above are all for object pointers, and the same applies to references.
The dynamic type and static type of pointers and references may be inconsistent, but the dynamic type and static type of objects are consistent.
D D;
D.DoSomething() and D.vfun() always call D::DoSomething() and D::vfun().
As for those things that are dynamically bound and those things that are statically bound, there is an article that summarizes it very well:
I summarized it in one sentence: only virtual functions use dynamic binding, and everything else is static binding. Certainly. So far I have not found anything that does not apply to this sentence. If there is an error, I hope you can point it out.
Something that needs special attention
When default parameters and virtual functions appear together, the situation is a bit complicated and it is easy to make mistakes. We know that virtual functions are dynamically bound, but for execution efficiency, default parameters are statically bound.

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();
Copy after login
From the above analysis, we can see that pD->vfun() and pB->vfun() both call function D::vfun(), but what are their default parameters?
Analysis, the default parameters are statically bound. When pD->vfun(), the static type of pD is D*, so its default parameter should be 20; similarly, pB->vfun The default parameter for () should be 10. I wrote the code and verified it, and it is correct.
I guess no one will like this feature. So, always remember:
"Never redefine function's inherited default parameters value."
About the c++ language
At present, I am basically in c++ Working under the subset "object-oriented programming", I don't know much about more complex knowledge. Even so, there are already a lot of things that need to be paid attention to when programming so far, and there may continue to be more in the future. This may be the reason why many people are opposed to C++.

c++ is one of Google's four official languages. However, Google has indeed launched the go language in recent years, and its positioning is similar to 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 an alternative language to c++. When you have time, you should learn about the Go language and see how it makes choices on issues like C++.

For more in-depth understanding of the application of dynamic binding and static binding in C++, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template