From: http://blog.csdn.net/wangyadong/archive/2009/03/27/4030024.aspx
In C ++, non-virtual functions are statically bound, while virtual functions are dynamically bound.
To better understand static binding and dynamic binding, we can look at the following example:
# Include <iostream> <br/> using namespace STD; <br/> Class B <br/> {<br/> Public: void fun () <br/>{< br/> cout <"I am from B" <Endl; <br/>}< br/>}; <br/> Class D: public B <br/> {<br/> Public: // void fun () <br/> // {<br/> // cout <"I am from D" <Endl; <br/> //} </P> <p> }; <br/> int main () <br/> {<br/> D x; <br/> B * pb = & X; <br/> Pb-> fun (); <br/> D * Pd = & X; <br/> Pd-> fun (); <br/> return 0; <br/>}
So are the two calls to the fun () function the same? Of course, if the fun () function is not defined in D (for example, in the example), the two calls will certainly be the same,:
I am from B
I am from B
However, the call process is different.
Pb-> fun (); // Pb is a pointer to type B and directly calls the fun () function in B.
Pd-> fun (); // PD is a pointer to the D type. The Compiler first finds the declaration of the fun () function in D, but not found, find the base class B in D, find fun (), and stop searching.
However, if D has its own defined fun () function, the execution result will be different:
Cancel the annotation in the preceding example and the result is:
I am from B
I am from D
Process:
Pb-> fun (); // call B: Fun ()
Pd-> fun (); // call D: Fun ()
The reason is that (non-virtual) Non-virtual functions are statically bound. That is to say, because Pb is declared as a pointer to type B, the non-virtual function called by PB) A non-virtual function is always the version defined by B, even if PB points to an object of the type "B's derived class D.
On the other hand, virtual functions are dynamically bound. No matter what type of pointer is used to call this virtual function, the virtual function is called Based on the object type actually pointed to by the pointer.
Is not related to the pointer type. If the fun () function is a virtual function, whether it is through PB or PD to call the fun () function, will call D: Fun (), because Pb AND Pd actually point
Are all objects of the same type D. See the following code:
Class B <br/> {<br/> Public: Virtual void fun () <br/> {<br/> cout <"I'm from B" <Endl; <br/>}< br/>}; <br/> Class D: Public B <br/>{< br/> Public: void fun () <br/>{< br/> cout <"I am from D" <Endl; <br/>}< br/>}; <br/> int main () <br/> {<br/> D x; <br/> B * pb = & X; <br/> Pb-> fun (); <br/> D * Pd = & X; <br/> Pd-> fun (); <br/> return 0; <br/>}
The fun function is declared as a virtual function in the program, so whether it is through PB or PD to call the fun () function, it will call D: Fun (), because Pb AND Pd actually point to the same type of D object. The output result of the program verifies this point:
I am from D
I am from D
Therefore, in C ++, do not redefine the inherited (non-virtual) Non-virtual functions. In this case, the determining factor of function calling is not the object itself, it is related to the pointer type of the function to be called, which leaves unpredictable risks to the program.
In the above example, any object d may show the behavior of B or D. the deciding factor is not the X object itself, but the pointer type pointing to X. At the same time, do not redefine the default parameter values of an inherited virtual function, because the default parameter values are static bindings, while the virtual function is dynamic binding.