A constructor cannot be virtual

久未见 提交于 2019-12-04 12:24:53

问题


In one of the C++ tutorials in internet, i found out the below description on why a constructor cannot be virtual

We cannot declare a virtual constructor. We should specify the exact type of the object at compile time, so that the compiler can allocate memory for that specific type.

Is this description correct ?

I am getting confused particularly with the phrase: so that the compiler can allocate memory for that specific type.


回答1:


As Bjarne himself explains here

A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only an interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a "call to a constructor" cannot be virtual.




回答2:


The constructor cannot be virtual because the standard says so.

The standard says so because it wouldn't make sense. What would a virtual constructor do?

Virtual methods are used in polymorphism... how should polymorphism work if you don't even have the objects yet?

We should specify the exact type of the object at compile time, so that the compiler can allocate memory for that specific type.

We should specify the exact type at compile time because we want an object of that type... I found their description very confusing too.

Also, in the paragraph it doesn't say this is the reason why constructors can't be virtual. It explains why virtual methods shouldn't be called from the constructor, but that's about it.




回答3:


How would a constructor be able to be virtual? virtual means that the result to a call to that function is determined by the dynamic type of the object. Before construction, there is no object to do this.

The way the tutorial phrases, what a constructor is, is also bogus. You need to specify the exact type, otherwise the thing you declare wont be considered a constructor and functions without a return type are not allowed.




回答4:


Just to add to what already been said, there is virtual constructor design pattern, also known as factory method or factory function:

... it deals with the problem of creating objects (products) without specifying the exact class of object that will be created




回答5:


It is correct, even though it misses the point in my humble opinion.

Constructors set up the virtual dispatching, i.e. point the right pointers at functions of the current class. If constructors could be virtual, who would set up the virtual constructor beforehand? There would be a horrible chicken-and-egg problem.

There is, however, an idiom named "virtual constructor", in which a static member of the class returns a base class pointer with a suitable class:

class A {
    static A* create();
    virtual ~A();
};

class B : public A { ... };

A* A::create() { return new B(); }


来源:https://stackoverflow.com/questions/8506938/a-constructor-cannot-be-virtual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!