Why cannot an inline function, constructor, or static member function be a virtual function?

Source: Internet
Author: User

Why cannot an inline function, constructor, or static member function be a virtual function?

1> inline functions

Inline functions are expanded during compilation, and virtual functions are dynamically integrated during runtime. Therefore, they cannot be defined as virtual functions.

2> Constructor

The constructor is used to create a new object, and the running of the virtual function is based on the object. When the constructor is executed, the object has not yet been formed, therefore, constructors cannot be defined as virtual functions.

3> static member functions

A static member function is a class rather than an object. Without this pointer, it cannot identify objects.

 

This can be understood from two perspectives:
1. Virtual means binding during execution. Therefore, during compilation, you must determine that the information cannot be virtual.
The constructor must construct an object at the time of compilation to execute the action.
The static member function does not belong to any object. The Compiling Time determines which one to execute when the execution does not exist.
Inline functions are actually virtual because they are recommended by the compiler.
2. Virtual means that the derived class can override its action.
The constructor of a derived class first executes the constructor of the base class instead of replacing the base class constructor. That is to say, the constructor of the base class can be regarded as a constructor of the derived class, so this function cannot be rewritten.
Static member functions do not belong to any object, so they cannot be rewritten.

Inline and virtual do not work at the same time. A function with virtual can be called in line without dynamic binding.

 

 

 

Virtual Constructor)

C ++ does not support direct virtual constructor. The virtual mechanism is designed to allow programmers to use objects without fully understanding the details (for example, only knowing the class implements a certain interface, but not knowing exactly what the class is. However, to create an object, you cannot just know what it is. You must fully understand all the details and clearly know what the object you want to create is. Therefore, constructors cannot be virtual. However, you can use the virtual clone () function (for the copy constructor) or virtual create (for the default constructor) function to obtain the effect of the virtual constructor. Note: The Return Value Type of the subclass member function clone () is intentionally different from that of the parent member function clone. This feature is called "covariant return types". This feature was not initially part of the C ++ language and is not supported by compilers of versions earlier than vc6.0.

 

Virtual destructor)

We recommend that you use a virtual destructor to delete a derived class object through a base class pointer. Code of the class to which the virtual function is bound to the object, rather than the pointer/referenced class. If the base class has a virtual destructor, When deleting baseptr (base class pointer), * the object type destructor of baseptr is called, instead of the destructor of the pointer type. Simply put, this class should have virtual destructor if it has virtual functions. Once you add a virtual function to the class, you have to pay the space cost for each object (each object has a pointer ), therefore, it is usually no extra effort to make the Destructor virtual. For classes with trivial and no subclass, virtual destructor only increase the overhead and do not use it.

Why cannot an inline function, constructor, or static member function be a virtual function?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.