为什么c++没有虚构造函数?


当前回答

c++的虚拟构造函数是不可能的。例如,不能将构造函数标记为虚函数。试试这段代码

#include<iostream.h>
using namespace std;
class aClass
{
    public:
        virtual aClass()
        {   
        }  
};
int main()
{
    aClass a; 
}

它会导致错误。这段代码试图将构造函数声明为虚函数。 现在让我们试着理解为什么要使用virtual关键字。Virtual关键字用于提供运行时多态性。例如,试试这段代码。

#include<iostream.h>
using namespace std;
class aClass
{
    public:
        aClass()
        {
            cout<<"aClass contructor\n";
        }
        ~aClass()
        {
            cout<<"aClass destructor\n";
        }

};
class anotherClass:public aClass
{

    public:
        anotherClass()
        {
            cout<<"anotherClass Constructor\n";
        }
        ~anotherClass()
        {
            cout<<"anotherClass destructor\n";
        }

};
int main()
{
    aClass* a;
    a=new anotherClass;
    delete a;   
    getchar(); 
}

In main a=new anotherClass; allocates a memory for anotherClass in a pointer a declared as type of aClass.This causes both the constructor (In aClass and anotherClass) to call automatically.So we do not need to mark constructor as virtual.Because when an object is created it must follow the chain of creation (i.e first the base and then the derived classes). But when we try to delete a delete a; it causes to call only the base destructor.So we have to handle the destructor using virtual keyword. So virtual constructor is not possible but virtual destructor is.Thanks

其他回答

你可以在@stefan的回答中找到一个例子和为什么不允许这样做的技术原因。在我看来,这个问题的合理答案是:

virtual关键字的主要用途是在我们不知道基类指针将指向什么类型的对象时启用多态行为。

但这是一种更原始的方式,对于使用虚拟功能,你需要一个指针。指针需要什么?一个指向的对象!(考虑程序正确执行的情况)

因此,我们基本上需要一个已经存在于内存某处的对象(我们不关心内存是如何分配的,它可能是在编译时或运行时),以便我们的指针能够正确地指向该对象。

现在,想象一下当要指向的类的对象被分配一些内存时的情况——>它的构造函数将在该实例本身被自动调用!

所以我们可以看到,我们实际上不需要担心构造函数是虚的,因为在任何情况下,你希望使用多态行为,我们的构造函数已经执行,使我们的对象准备使用!

c++的虚拟构造函数是不可能的。例如,不能将构造函数标记为虚函数。试试这段代码

#include<iostream.h>
using namespace std;
class aClass
{
    public:
        virtual aClass()
        {   
        }  
};
int main()
{
    aClass a; 
}

它会导致错误。这段代码试图将构造函数声明为虚函数。 现在让我们试着理解为什么要使用virtual关键字。Virtual关键字用于提供运行时多态性。例如,试试这段代码。

#include<iostream.h>
using namespace std;
class aClass
{
    public:
        aClass()
        {
            cout<<"aClass contructor\n";
        }
        ~aClass()
        {
            cout<<"aClass destructor\n";
        }

};
class anotherClass:public aClass
{

    public:
        anotherClass()
        {
            cout<<"anotherClass Constructor\n";
        }
        ~anotherClass()
        {
            cout<<"anotherClass destructor\n";
        }

};
int main()
{
    aClass* a;
    a=new anotherClass;
    delete a;   
    getchar(); 
}

In main a=new anotherClass; allocates a memory for anotherClass in a pointer a declared as type of aClass.This causes both the constructor (In aClass and anotherClass) to call automatically.So we do not need to mark constructor as virtual.Because when an object is created it must follow the chain of creation (i.e first the base and then the derived classes). But when we try to delete a delete a; it causes to call only the base destructor.So we have to handle the destructor using virtual keyword. So virtual constructor is not possible but virtual destructor is.Thanks

当人们问这样的问题时,我喜欢对自己说:“如果这真的是可能的,会发生什么?”我真的不知道这意味着什么,但我猜这可能与能够基于所创建对象的动态类型重写构造函数实现有关。

我看到了一些潜在的问题。首先,在调用虚构造函数时,派生类不会被完全构造,因此实现中存在潜在的问题。

其次,在多重继承的情况下会发生什么?你的虚构造函数可能会被多次调用,然后你需要有某种方法知道哪个被调用了。

第三,一般来说,在构造时,对象并没有完全构造虚拟表,这意味着需要对语言规范进行很大的更改,以允许在构造时就知道对象的动态类型。这将允许基类构造函数在构造时调用其他虚函数,使用未完全构造的动态类类型。

最后,正如其他人指出的那样,您可以使用静态的“create”或“init”类型函数实现一种虚拟构造函数,基本上与虚拟构造函数所做的事情相同。

When a constructor is invoked, although there is no object created till that point, we still know the kind of object that is gonna be created because the specific constructor of the class to which the object belongs to has already been called. Virtual keyword associated with a function means the function of a particular object type is gonna be called. So, my thinking says that there is no need to make the virtual constructor because already the desired constructor whose object is gonna be created has been invoked and making constructor virtual is just a redundant thing to do because the object-specific constructor has already been invoked and this is same as calling class-specific function which is achieved through the virtual keyword. Although the inner implementation won’t allow virtual constructor for vptr and vtable related reasons.

Another reason is that C++ is a statically typed language and we need to know the type of a variable at compile-time. The compiler must be aware of the class type to create the object. The type of object to be created is a compile-time decision. If we make the constructor virtual then it means that we don’t need to know the type of the object at compile-time(that’s what virtual function provide. We don’t need to know the actual object and just need the base pointer to point an actual object call the pointed object’s virtual functions without knowing the type of the object) and if we don’t know the type of the object at compile time then it is against the statically typed languages. And hence, run-time polymorphism cannot be achieved. Hence, Constructor won’t be called without knowing the type of the object at compile-time. And so the idea of making a virtual constructor fails.

“构造函数不能是虚拟的”

有一些合理的理由可以证明这种说法是正确的。

要创建对象,对象类的构造函数必须与类的类型相同。但是,对于虚拟实现的构造函数,这是不可能的。 在调用构造函数时,还没有创建虚表来解析任何虚函数调用。因此,虚拟构造函数本身将没有任何可以查找的地方。


因此,不可能将构造函数声明为虚函数。