Explore what functions in C++ cannot be declared virtual

  • 2020-05-10 18:34:49
  • OfStack

Common functions that cannot be declared as virtual are: ordinary functions (non-member functions); Static member function; Inline member function; Constructor; Friend function.

1. Why does C++ not support ordinary functions as virtual functions?

Normal functions (non-member functions) can only be overload, not override, and there is no point in declaring them as virtual functions, so the compiler punts them at compile time.

The runtime behavior of polymorphism is embodied in virtual functions, which exhibit polymorphism through inheritance, the top layer

Functions are not member functions and cannot be inherited

2. Why does C++ not support virtual constructors?

The reason is simple, mostly semantic, and therefore not supported. Because constructors are created to explicitly initialize object members, virtual function is designed to properly handle objects without fully understanding the details. In addition, the virtual function generates different actions in different types of objects. Now the object has not been generated yet. How to use the virtual function to complete the actions you want to complete? (this is not a typical paradox.)

(1) the constructor cannot be inherited, so it cannot be declared as virtual

(2) constructor 1 is generally used to initialize the object, which can only be polymorphic after 1 object is generated

If the constructor is declared as virtual, it will appear as if the object has not yet been generated

In this case, the polymorphic mechanism is used, so it is not feasible.

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, to reduce the cost of function calls, virtual function is in order to inherit the object can accurately perform their own actions, which is impossible to unify 1. (again, the inline function is expanded at compile time, and the virtual function is dynamically bound at run time.)

There is an essential difference between inline function and virtual function. inline function is expanded when the program is compiled and replaced with the whole function body at the function call, while virtual function can determine how to call at the run time. Therefore, inline function represents a compile-time mechanism, while virtual function represents a run-time mechanism. In addition, the one-slice virtual function cannot be an inline function.

4. Why does C++ not support static member functions as virtual functions?

This is also very simple, static member functions have only one piece of code per class, all objects share this one piece of code, and there is no need to dynamically define it. Can't be inherited, only belongs to the class.

5. Why does C++ not support virtual functions for friend functions?

Since C++ does not support inheritance of friend functions, there is no such thing as virtual functions for functions without inheritance properties. A member function that does not belong to a class and cannot be inherited.

eg:

 


/* 
 * main.cpp 
 * 

 *   Author: china 
 */ 
#include <iostream> 
using namespace std; 
class B { 
public: 
  B() { 
    cout << " The base class structure " << endl; 
  } 
  /* In class inheritance, if there is a base class pointer to a derived class, use the base class pointer delete , the derived part of a derived class cannot be destructed if it is not defined as a virtual function.  
   *  You can! virtual Try to get rid of 1 Under the  
   *  Therefore, in the inheritance system of the class, the destructor of the base class is not declared as virtual function, which is easy to cause memory leak. So if you design 1 If a definite class may be a base class, you must declare it as a virtual function.  
   * */ 
  virtual ~B() { 
    cout << " The base class destructor " << endl; 
  } 
  virtual void func() { 
    cout << " The base class func()" << endl; 
  } 
 
private: 
}; 
class D :public B{ 
public: 
  D() { 
    cout << " Derived class construct " << endl; 
  } 
  ~D() { 
    cout << " Derived class destructor " << endl; 
  } 
  void func() { 
    cout << " The derived class func()" << endl; 
  } 
 
private: 
}; 
int main(int argc, char **argv) { 
 
  D d; // The call construct has an object first  
  B*p = &d; 
  p->func(); // Again, the polymorphic mechanism  
  p = new D(); // Recall construct  
  p->func(); // Again, the polymorphic mechanism  
  delete p; 
  return 0; 
} 

Related articles: