Common functions that cannot be declared as virtual functions are: ordinary function (non-member function), static member function, inline member function, constructor, friend function.
1. Why does C + + not support ordinary functions as virtual functions?
The normal function (non-member function) can only be overload, cannot be override, declared as virtual function also has no meaning, so the compiler will compile the Shibangding function.
2. Why does C + + not support a constructor function as a virtual function?
The reason is very simple, mainly from the semantic considerations, so it is not supported. Because constructors are originally created to explicitly initialize object members, virtual function is primarily designed to handle objects correctly without fully knowing the details. In addition, the virtual function produces different actions on different types of objects, and now the object has not been generated, how to use the virtual function to complete the action 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 to expand directly in the code, reduce the cost of function calls, virtual function is to inherit after the object can accurately perform their own actions, it is impossible to unify. (again, the inline function is expanded at compile time, and virtual functions can be dynamically determined at runtime)
4. Why does C + + not support static member functions as virtual functions?
This is also very simple, static member functions for each class has only one copy of the code, all the objects share this code, he also did not want to dynamically state the necessity.
5. Why does C + + not support friend function as virtual function?
Because C + + does not support the inheritance of friend functions, there is no virtual function for functions that do not inherit attributes.
*********************************************************************
1, the top-level function : The behavior of the polymorphic operation is embodied in the virtual function, virtual function through inheritance to reflect the multi-state function, the top-level function does not belong to the member function, it can not be inherited.
2. Constructors : (1) constructors cannot be inherited and cannot be declared as virtual functions.
(2) Constructors are generally used to initialize objects, only after an object is generated, to play a polymorphic role, if the constructor is declared as virtual function, it is shown that the object has not been generated in the case of the use of polymorphic mechanisms, it is not feasible, the following example:
[CPP]View Plaincopy
- #include <iostream>
- Using namespace std;
- Class B
- {
- Public
- B () {}
- virtual void Show ()
- {
- cout<<"* * *" <<endl;
- }
- };
- Class D: PublicB
- {
- Public
- D () {}
- void Show ()
- {
- cout<<"= = =" <<endl;
- }
- };
- int main (void)
- {
- B *PB;
- D D; //Mr. Cheng Object
- pb=&d;
- Pb->show (); //re-expression polymorphism
- pb=new D (); //Call constructor first
- Pb->show (); //re-polymorphic
- Delete PB;
- return 0;
- }
3. Static function: cannot be inherited, only belongs to this class.
4, friend function: The friend function does not belong to the class member function, cannot be inherited.
5, the inline function: the inline function and the virtual function have an essential difference, the inline function is expanded when the program is compiled, at the function call with the entire function body to replace, and the virtual function is in the runtime to determine how to call, Thus, the inline function embodies a compile-time mechanism, and the virtual function is a kind of running-time mechanism. In addition, all virtual functions cannot be inline functions.
Functions in C + + functions that cannot be declared as virtual functions