Why are there no virtual constructors in C++?

Yeah, it was an interview question. I’m not sure I can even understand the concept, or why you would want to do such a thing.

Oh, and while I’m asking, anyone know of a really good website that summarizes the languages features and can be used for a good review?

Virtual destructors exist because of a situation like this:

class A;
class B : public A;

int main()
    A* a = new B;
    delete a;

    return 0;

In this case, because the destructors of A and B are not marked virtual, the destructor of B will not be invoked. After all, a is an A.

The only reason to have virtual constructors is for a situation where you need to create an object without specifying its type. That’s just not legal according to the language rules, so there’s no need for such a thing.

What ultrafilter said. I don’t have any cite to prove it, but I think that the creators of C++ just couldn’t think of a real-world instance where you need to create an object but don’t know its type.

'course nowadays, there’s plenty of times where you might want to do just that. And there’s ways to do it, the most common of which (at least, in my world) is a Factory pattern.

As far as a good website that summarizes C++ - sheesh, that’s big. What do you need it for? A programming job? Just need to appear intelligent about C++? Does it have to be C++, or is it OO that you really want to learn?

Not to be too picky here, but even with a factory, the type of the object created is completely specified at the point that it’s created. The only difference is that the object’s type is not (necessarily) visible to the rest of the program.

Yes to both!

Bjarne Stroustrup’s “The C++ Programming Language” is the authoritative reference on C++.

The problem with a “virtual constructor” is that SOMETHING needs to know what it’s constructing! Pure virtual functions are still part of a constructed class…they exist as pointers in a “virtual table”, which table is then filled out when the more specific class is created. But the process of creation itself can’t be made virtual.

And that was an interview question for me once, too.