Common functions that cannot be declared as virtual functions include common functions (non-member functions), static member functions, inline member functions, constructor functions, and youyuan functions.
1. Why does C ++ not support common functions as virtual functions?
A normal function (non-member function) can only be overload and cannot be override. it is meaningless to declare it as a virtual function. Therefore, the compiler will specify a function during compilation.
2. Why does C ++ not support constructor as a virtual function?
The reason is very simple, mainly in terms of semantics, so it is not supported. This is because the constructor was originally created to explicitly initialize object members. However, virtual functions are mainly used to process objects correctly without fully understanding the details. In addition, virtual functions generate different actions for different types of objects. Currently, objects are not generated. How to use virtual functions to complete the actions you want to complete. (This is not a typical paradox)
3. Why does C ++ not support inline member functions as virtual functions?
In fact, it is very simple. The inline function is designed to expand directly in the Code and reduce the cost of function calling. The virtual function is designed to accurately execute its own actions after inheritance, this cannot be unified. (Besides, the inline function is expanded during compilation, and the virtual function can be dynamically bound during runtime)
4. Why does C ++ not support static member functions as virtual functions?
This is also very simple. static member functions only have one copy of code for each class. All objects share the code, and they do not need to be dynamic.
5. Why does C ++ not support user functions as virtual functions?
Because c ++ does not support the inheritance of functions, there is no virtual function for functions without inheritance features.
**************************************** *****************************
1,Top-level functions: The runtime behavior of polymorphism is reflected in virtual functions. virtual functions are inherited to reflect polymorphism. Top-level functions are not member functions and cannot be inherited.
2,Constructor(1) constructor cannot be inherited, so it cannot be declared as a virtual function.
(2) constructor is generally used to initialize an object. Only after an object is generated can the constructor perform polymorphism. If the constructor is declared as a virtual function, it is shown that the polymorphism mechanism is used when the object is not generated, and thus it does not work, as shown in the following example:
# Include <iostream> using namespace STD; Class B {public: B () {}virtual void show () {cout <"***" <Endl ;}}; class D: Public B {public: D () {} void show () {cout <"=" <Endl ;}}; int main (void) {B * pb; d; // The adult PB = & D; Pb-> show (); // then the polymorphism Pb = new D (); // call the constructor Pb-> show (); // then perform the multi-state Delete Pb; return 0 ;}
3. Static functions: they cannot be inherited and only belong to this class.
4. Friend functions: A friend functions are not a member function of the class and cannot be inherited.
5. inline functions: inline functions are essentially different from virtual functions. inline functions are expanded when the program is compiled and replaced with the entire function body during function calls, virtual functions can determine how to call them at runtime. Therefore, inline functions reflect a compilation mechanism, while virtual functions reflect a runtime mechanism. In addition, all virtual functions cannot be inline functions.