Constructor cannot be a virtual function

Source: Internet
Author: User
The reason the constructor cannot be declared as a virtual function:

1, the so-called virtual function is a polymorphic case only executes one, and from the concept of inheritance, always first constructs the parent class object, then can make the child class object, if the constructor is set to the virtual function, then you must construct the parent class's constructor to have the explicit call constructs, also has one reason is to prevent the error the occurrence, Imagine if you accidentally rewrite a function like a parent constructor in a subclass, then your parent class constructor will be overwritten, that is, you cannot complete the construction of the parent class, and you will get an error.

Class A
{
Public:
Virtual A ()
{
cout<< "A" <<endl;
}

};
Class B:public A
{
Public
B ()
{
cout<< "B" <<endl;
}


};

At the compile point, it has been indicated that it cannot be passed, removing virtual

2, the main meaning of the virtual function is inherited by the derived class to produce polymorphism, the constructor of the derived class, the compiler will join the construction of the base class code, if the base class constructor used parameters, the derived class in its constructor must give the base class parameters, that is, the reason is a bit around, That is, if the constructor of a derived class must be the same as the parent class, this is obviously not true.

Of course there are other explanations: 1, from the storage space point of view, the virtual function corresponds to a vtable, but this vtable is actually stored in the object's memory space. The problem comes out, if the constructor is virtual, it needs to be called through vtable, but the object is not instantiated, that is, the memory space, no, cannot find vtable, so the constructor cannot be a virtual function.

2, from the use of angle 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, the use of virtual functions there is no instance meaning, the first instance is not initialized, how to polymorphic it. A virtual function can become the member function that invokes a subclass when it is invoked by a pointer or reference to the parent class. 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.

3, from the implementation, vtable after the constructor call is established, and therefore the constructor is not virtual function, from the actual meaning of the call to the constructor can not determine the true type of the object (subclass to invoke the constructor of the parent class), and the constructor provides initialization, the object lifetime is only executed once, is not a dynamic behavior of an object, nor is it necessary to become a virtual function.

  • Related Article

    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.