为什么虚拟构造函数不存在? [英] Why virtual constructor does not exist?

查看:105
本文介绍了为什么虚拟构造函数不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您能帮我为什么C ++中没有虚拟构造函数吗?

谢谢,
Mahesh

Hi,

Could you please help me why there is no virtual Constructor in c++?

Thanks,
Mahesh

推荐答案

您错了.它存在.它已经(并且应该被)广泛使用,例如,参见:旧的新事物:何时应该使用析构函数进行虚拟? [ ^ ].
:)
You are wrong. It exists. And it is (well, it should be) widely used, see, for instance: The Old New Thing: When should your destructor be virtual?[^].
:)


它们可以-例如:

They do - for example:

class test_interface
{
    public:
        virtual ~test_interface() {}
};

class A : public test_interface
{
    public:
        virtual ~A() { std::cout << "Running A''s destructor..." << std::endl; }
};

class B : public test_interface
{
    public:
        virtual ~B() { std::cout << "Running B''s destructor..." << std::endl; }
};

int main()
{
    test_interface *pA = new A;
    test_interface *pB = new B;

    delete pA;  // Virtual call of the destructor A::~A
    delete pB;  // Virtual call of the destructor B::~B
}



如果您从基类析构函数的声明中删除了虚函数,则输出将停止(尽管确切发生的事情是未定义的,应该指出).

干杯,



PS回应以下评论...

正如Sauro指出的那样,要创建类的对象,您确实需要在创建对象之前先知道对象的类型.但是...您不一定总是需要知道所创建对象的类型.例如,您可能希望能够在不知道对象具体类型的情况下多态复制对象,例如:



If you remove the virtual from the base class destructor''s declaration the output will stop (although exactly what happens is undefined, should have pointed that out).

Cheers,

Ash

PS in response to the comment below...

Well as Sauro''s pointed out to create an object of a class you really need to know what type the object is going to be before you can create it. However... you don''t always need to know the type of the object you''re creating. For example you might want to be able to polymorphically copy objects without knowing what concrete type they are, e.g:

void A::cache_companion( companion_interface *ci )
{
    // Want a deep copy of the object pointed to by ci
    // but we don''t know its concrete class!
}



那么如何解决呢?好吧,第一件事就是定义接口的多态复制成员:



So how can you get around that? Well the first thing is you define a polymorphic copy member of the interface:

class companion_interface
{
    public:
        virtual companion_interface *polymorphic_copy() const = 0;
};


然后可以实现它:


You can then implement it:

class implemented_interface : public companion_interface
{
    public:
        virtual implemented_interface *polymorphic_copy() const
        {
            return new implemented_interface( *this );
        }
};



然后,您可以从之前完成代码:



You can then complete the code from earlier:

void A::cache_companion( companion_interface *ci )
{
    cached_interface_ = ci->polymorphic_copy();
}



因此,C ++确实不直接支持虚拟构造函数,仅在可以将构造对象的类型与构造对象分离的情况下才支持虚拟构造函数.这项技术之所以有效,是因为即使对象无法以有意义的方式告诉其他人对象,他们也总是知道他们自己的类.



So C++ does support virtual constructors, just not directly and only in the cases where the type of the object being constructed can be decoupled from the thing doing the construction. This technique works because objects always know their own class even if they can''t tell anyone else what it is in a meaningful way.


假设我们正在谈论虚拟构造函数,这里是该男子本人的解释:

为什么我们没有虚拟构造函数?
[^ ]:

虚拟呼叫是一种在给出部分信息的情况下完成工作的机制.特别是,虚拟"使我们可以调用仅知道接口而不知道对象确切类型的函数.要创建对象,您需要完整的信息.特别是,您需要知道要创建的确切类型.因此,对构造函数的调用"不能是虚拟的.
Assuming we are talking about virtual constructors, here is the explanation from the man himself:

Why don''t we have virtual constructors?
[^]:

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.


这篇关于为什么虚拟构造函数不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆