Why constructors, inline functions, static functions, and friend functions cannot be virtual functions

Source: Internet
Author: User

Why a constructor cannot be a virtual function


  1. From the storage space point of view, the virtual function corresponds to avtableA pointer to a virtual function table, which we all know, but this pointvtableThe pointer is actually stored in the object's memory space. The problem is that if the constructor is virtual, you need to passvtableTo invoke, but the object is not instantiated, that is, the memory space is not yet, how to findvtableIt? So the constructor cannot be a virtual function. In simple terms:the execution of virtual functions depends on the virtual function table. The virtual function table is initialized in the constructor, which is initialized vptr to point to the correct virtual function table. During the construction of the object, the virtual function table is not initialized and will not be able to proceed.

  2. From the perspective of Use, virtual function is mainly used in the case of incomplete information, can make the overloaded function to get the corresponding call. The constructor itself is to initialize the instance, and it has no practical meaning to use virtual functions. So the constructor is not necessarily a virtual function. The function of a virtual function is to call it through a pointer or reference to the parent class to become the member function that invokes the subclass. The constructor is called automatically when the object is created and cannot be called by a pointer or reference to the parent class, so the constructor cannot be a virtual function. Therefore, it is not necessary to make the constructor a virtual function .

  3. The constructor does not need to be a virtual function, nor is it allowed to be a virtual function, because we always explicitly specify the type of object when creating an object, although we may access it through pointers or references to the base class in the lab. But destruction is not necessarily, we tend to destroy objects through the base class pointers. At this point, if the destructor is not a virtual function, the object type cannot be correctly identified and the destructor cannot be called correctly.

  4. From the implementation, it is vbtl established after the constructor is called, so the constructor cannot be a virtual function from the actual meaning, the real type of the object cannot be determined when the constructor is called (because the subclass will tune the constructor of the parent class), and the function of the constructor is to provide initialization, The object lifetime is only executed once, not the object's dynamic behavior, nor is it necessary to become a virtual function .

  5. When a constructor is called, one of the first things it does is initialize itVPTR。 Therefore, it can only know that it is the "current" class, and completely ignores whether there are successors behind the object. When the compiler generates code for this constructor, it generates code for the constructor of the class--is neither a base class nor a derived class for it (because the class does not know who inherits it)。 So it uses theVPTRIt has to be for this class.VTABLE。 And, as long as it's the last constructor call, in this object's lifetime,VPTRwill remain initialized to point to thisVTABLE, but if a later derived constructor is called, the constructor sets theVPTRPoint to itVTABLE, etc. until the end of the constructor function.VPTRState is determined by the constructor that was last called. (the constructor of the base class is called multiple times, such as itself and its derived classes, and each invocation VPTR may produce a differentThis is why the constructor call is another reason for the order of the base class to the more derived class. However, when this series of constructor calls is occurring, each constructor is already setVPTRPoint it to its ownVTABLE。 If a function call uses a virtual mechanism, it will only be generated through its ownVTABLEThe call, not the lastVTABLE(all constructors are called before the lastVTABLE)。

inline,static,constructorNone of the three functions can bevirtualKey word.
inlineis the compile-time expansion, must have the entity;
staticBelong toclassOwn, also must have the entity;
virtualfunction is based onvtable(Memory space),constructorfunction if it isvirtual, the call is also subject to thevtableLooking for, butconstructorIsvirtualThe case is not found, becauseconstructoritself does not exist, create aclassInstances, no instances,classThe members (exceptpublic static/protected static for friend class/functions, the rest whether or notvirtual) cannot be accessed.

C++Those in the function can not be declared as virtual functions

Common can not be declared as virtual functions are: ordinary functions (non-member functions); static member functions; inline member functions; constructors; friend functions.  
1. Why c++ does not support normal functions as virtual functions?  
Ordinary functions (non-member functions) can only be overload , cannot be override , which is declared as a virtual function, has no meaning, so the compiler will statically bind the function at compile time.

  1. Why c++ does not support constructors as virtual functions?  
    The reason is simple, mostly semantically, and therefore not supported. Because constructors are originally created to explicitly initialize object members, however, virtual function is primarily intended to handle objects correctly without fully understanding the details. In addition, virtual function is to produce different actions on different types of objects, now the object has not been produced, how to use virtual function to complete the action you want to complete. (This is not a typical paradox)

  2. Why is an C++ inline member function not supported as a virtual function?
    Inline functions are intended to be expanded directly in the code, reducing the cost of function calls, and virtual functions are not unified in order to perform their actions accurately after inheritance. (Again, the inline function is expanded at compile time, and virtual functions can be dynamically determined at runtime)

  3. Why C++ are static member functions not supported as virtual functions?
    Static member functions have only one piece of code for each class, and all of the objects share this code, and he does not need to be dynamically state-determined.

  4. Why is C++ a friend function not supported as a virtual function?
    Because the C++ inheritance of friend functions is not supported, there is no virtual function for functions that do not inherit attributes.

Why constructors, inline functions, static functions, and friend functions cannot be virtual functions

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.