A constructor cannot be virtual
Asked Answered
R

5

5

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.

Retention answered 14/12, 2011 at 15:18 Comment(9)
The explanation is half right, half bogus. A constructor can't be virtual because it simply wouldn't make sense.Dicho
"so that the compiler can allocate memory for that specific type" is more or less gibberish. We have to specify the type when we create the object, because every object has a type. (Of course, as part of creating the object, some memory has to be allocated depending on the size of that type; but that's not really relevant to specifying the object's type).Heterogenesis
@MikeSeymour : Then what about the dynamically created object. Does the compiler need to know the type of the object then also?Retention
Yes, to create an object, the compiler must know the complete type.Varipapa
Does it need to know the type of object at compile time itself, even for dynamically created object?Retention
@LinuxPenseur: Yes, in C++ objects can only be created when the type is known at compile time. Objects are only created by declarations (which include the type), and new expressions (which also include the type).Heterogenesis
@MikeSeymour : Exact type of the object determination is done through the call to the constructor?Retention
@LinuxPenseur: No, it's determined at compile time by the programmer specifying it in the declaration or new-expression. (e.g. Thing t; or new Whatever). The compiler generates code based on this to allocate the right amount of memory, and call the correct constructor.Heterogenesis
You may want to look into the virtual constructor idiom, which is basically a virtual named copy constructor. There's (rare) times when it's handy.Varipapa
D
19

As Bjarne himself explains in his C++ Style and Technique FAQ:

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.

Deposal answered 14/12, 2011 at 15:22 Comment(6)
Thanks. Could you please explain this based on so that the compiler can allocate memory for that specific type. Is the phrase correct? Then what about the dynamically allocated memory for an object?Retention
@LinuxPenseur: When dynamically creating an object, the compiler still needs to know the size of the object. For this, it needs to know the actual type.Scalp
@LinuxPenseur: The statement from Bjarne answers that Q of yours, virtualism(dynamic dispatch) allows you to call a function based on type of an object, however the object is completely formed only after the constructor call returns,So unless the object is completely formed how can you call a function based on it?Deposal
@BjörnPollex : Oh!!. Compiler still needs to know the size of the object, when dynamically creating an object? I never thought of this. Are you sure about this as i am going to change my understanding based on your word?Retention
@LinuxPenseur: I am 100% sure of it. This is the reason why you cannot create an instance of an incomplete type (such as a forward-declared class) - neither statically nor dynamically.Scalp
@LinuxPenseur: Ofcourse it does! How would it allocate enough memory for the object it won't know the size of the object?Deposal
C
3

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.

Chilpancingo answered 14/12, 2011 at 15:22 Comment(0)
Q
1

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(); }
Quintin answered 14/12, 2011 at 15:23 Comment(5)
The virtual-constructor idiom is used for creating copies of objects from base-class references. The way you show it, it is not very useful.Scalp
@BjörnPollex: You mean a virtual function that returns a copy of the object it is invoked on, right? I know this concept under the term "virtual copy constructor". The concept I've demonstrated can get useful to hide the implementation classes completely from calling code.Quintin
This makes some sense. Although this case does not involve anything virtual (other than the polymorphic return-type). In fact, for your code to work, A::create would have to be static, which is mutually exclusive with virtual.Scalp
I just checked, the C++ FAQ calls both variants the virtual constructor idiom. Although your example would still have to be modified. A::create would have to be virtual (or pure virtual is A is abstract), and would have to be overriden in B.Scalp
@BjörnPollex: Yes, the static is missing (and crucial), sorry. Edited. And yes, "virtual constructors" are not really virtual, but try to emulate similar behaviour (function call on base class with partial knowledge of the class) to virtual functions.Quintin
R
1

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.

Ramie answered 14/12, 2011 at 15:24 Comment(0)
L
1

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

Lille answered 14/12, 2011 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.