Dynamic binding occurs only when you call a virtual member function, that is, only in the implementation phase of the program to know which version of the virtual function calls the base class pointer / reference. In this case the dynamic call object for the type of object pointers / references. (When the static type is a type definition, which is the base class type here, static and dynamic types generally the same, only in this case the two may be different.)
OOP simply "polymorphism" the core idea (Object Oriented Programming) is that the above process.
The derived class overrides points
When the derived class virtual function to cover the base class virtual function, parameters must match exactly (otherwise it would overload a function defined in the derived class). Press c 11 standard ++ virtual functions can be shaped brackets after the parameter added to the derived class want to overwrite the override
keywords, let the compiler help check.
* Avoid dynamic binding
You can use the scope operator (: :) dynamic binding mechanism to circumvent virtual function, which is usually derived class virtual function to call it covers the base class virtual function in time.
* Prohibit inheritance and coverage
Plus final
qualifiers can disable inheritance and coverage
class A final{/* */}; // 类A无法被继承
If all members of a class are public
, you can use struct
instead of the difference between the two is that the permissions of members
struct B{
virtual void fun() final; //虚函数fun()无法被派生类覆盖
}
struct C:B{}
void fun(); //报错,无法覆盖