Recommended study: "c Tutorial"
——The compiler uses static binding for non-virtual methods (compile-time matching ), use dynamic binding (runtime matching) for virtual methods.
Static binding is more efficient than dynamic binding.
How virtual functions work.
Virtual function.
Redefine member functions (change function signatures).
Redefine overloaded member functions.
Efficiency
In order for the program to make decisions during the runtime phase, some method must be used to track the type of object pointed to by the base class pointer or reference , which adds additional processing overhead. Therefore, the following situations are more suitable for static binding:
# therefore static binding is set as the default choice for C.
If you want to redefine a base class method in a derived class, set it to a virtual method; otherwise, set it to a non-virtual method.
How virtual functions work
The way the compiler handles virtual functions is to add a hidden member to each object. The hidden member stores a pointer to the function address array. This array is called a virtual function table (vtbl), which stores the addresses of virtual functions declared for class objects.
The derived class object will contain a pointer to an independent address table (that is, a new table is created). (Increased memory overhead)
When calling a virtual function, the program will look at the vtbl address stored in the object, then turn to the corresponding function address table and look up the address in the table. (Affects execution speed)
In short, using virtual functions will have a certain cost in memory and execution speed; even if non-functions are slightly more efficient than virtual functions, they do not have dynamic linkage. Edit function.
The constructor cannot be a virtual function.
The destructor should be a virtual function unless the class does not need to be a base class.
Friend functions cannot be virtual functions, because friends are not class members, and only members can be virtual functions.
If the derived class does not redefine the function, the base class version of the function will be used (inherits it). If a derived class is in a derivation chain, the latest virtual function version (pointer or reference call) will be used, unless the base class version is hidden.
Redefining will hide the base class method:
class Dwelling {public: virtual void showperks(int a) const; ... };class Hovel : public Dwelling {public: virtual void showperks() const; ... }
Redefining the function in the derived class (changing the parameter signature) will hide it A base class method with the same name, rather than overloading a base class method.
Hovel trump; trump.showperks(); // validtrump.showperks(5); // invalid
If you redefine an inherited method, make sure it is exactly the same as the original prototype. If the return type is a base class reference or pointer, it can be modified to a reference or pointer to a derived class (return type covariance: that is, the return type is allowed to change as the class type changes).
If the base class declaration is overloaded, all base class versions should be redefined in the derived class; if only one version is defined, the other versions will be hidden. Class objects will not be able to use them.
class Dwelling {public: virtual void showperks(int a) const; virtual void showperks(double x) const; virtual void showperks() const; ... };class Hovel : public Dwelling { virtual void showperks(int a) const; virtual void showperks(double x) const; virtual void showperks() const; ... };
If no modification is required, the new definition can only call the base class version:
<span class="cnblogs_code"><span style="color: #0000ff;">void </span> Hovel::showperks()<span style="color: #0000ff;">const</span> {Dwelling::showperks();}</span>
The above is the detailed content of Detailed explanation of C++ virtual member functions and dynamic binding. For more information, please follow other related articles on the PHP Chinese website!