To support the polymorphism of C + +, dynamic binding and static binding are used.
1. Static type of object: the type that the object takes when declaring. is determined at compile time.
2. The object's dynamic type: The declaration of the object currently referred to. 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:
classa{};classB: Publica{};classC: Publica{};intMain () {C(p!=NewC ();//the static type of the PC is the type c* it declares, and the dynamic type is also c*A *pa=pc;//the static type of the PA is the type of object that it declares the type of A*,PA that the dynamic type is pointing to. A *B *pb=NewB (); PA=PB;//The dynamic type of PA can be changed, and now its dynamic type is b* return 0;}
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.
classa{ Public: voiddosomething () {cout<<"A"<<Endl; } Virtual voidFun () {cout<<"Virtual A"<<Endl; }};classB: Publica{ Public: voiddosomething () {cout<<"B"<<Endl; } Virtual voidFun () {cout<<"Virtual B"<<Endl; }};classC: Publica{ Public: voiddosomething () {cout<<"C"<<Endl; } Virtual voidFun () {cout<<"Virtual C"<<Endl; }};intMain () {C(p!=NewC ();//the static type of the PC is the type c* it declares, and the dynamic type is also c*Pc->dosomething ();//CPc->fun ();//Virtual CA *pa=pc;//the static type of the PA is the type of object that it declares the type of A*,PA that the dynamic type is pointing to. A *Pa->dosomething ();//A①Pa->fun ();//Virtual CB *pb=NewB (); PB->dosomething ();//BPb->fun ();//Virtual BPA=PB;//The dynamic type of PA can be changed, and now its dynamic type is b*Pa->dosomething ();//A②Pa->fun ();//Virtual B return 0;}
DoSomething () is a non-virtual function, which is statically bound, that is, at compile time according to the object's static type to select the function, so, PA, Pb, PC calls are their own dosomething () function, but for ① in the PA's fun () Function and the PA's fun () function in ②, because fun () is a virtual function, they are bound to a dynamic object, so ① PA calls is the fun () function of the PC, ② PA call is the PB fun () function.
It is important to note that:
When the default parameters and virtual functions appear together, the situation is a bit complicated, because virtual functions are dynamically bound, but in order to perform efficiency, the default parameters are statically bound.
classa{ Public: Virtual voidFunintI=Ten) {cout<<"Virtual A"<<i<<Endl; }};classB: Publica{ Public: Virtual voidFunintI= -) {cout<<"Virtual B"<<i<<Endl; }};intMain () {B*b=NewB (); A*a=b; b->fun ();//Virtual BA->fun ();//Virtual B Ten return 0;}
B->fun (), A->fun () is called the Fun () function of B, but the default function is statically bound, so A->fun () is called the default value of A's virtual function fun () 10,b->fun () called the virtual function of B () the default value of 20.
Dynamic binding exists only where virtual functions are involved!!!!!
Reference Blog: 6427731
Dynamic binding and static binding for C + +