虚拟构造函数

发布于 2024-11-15 06:11:46 字数 66 浏览 2 评论 0原文

我想知道虚拟构造函数的含义是什么以及如何使用它。

另外我知道C++不允许虚拟构造函数,我想知道为什么。

I was wondering what is the meaning of a virtual constructor and how would it be used.

In addition I know that C++ does not allow for a virtual constructor, and I was wondering why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年少掌心 2024-11-22 06:11:46

C++ 不允许虚拟构造函数,因为您首先需要一个对象来调用虚拟方法!

术语虚拟构造函数用于习语 和众所周知的设计模式。这个习惯用法/模式涉及工厂的定义:具有虚拟方法的中间对象,其作用是创建相关对象。由于该方法是虚拟的,并且其目的是创建对象,因此它被昵称为“虚拟构造函数”。

C++ does not allow virtual constructors because you need an object to invoke a virtual method in the first place!

The term virtual constructor is used for for idiom and a well-known design pattern. This idiom/pattern involves the definition of factory: an intermediate object with a virtual method who's role is to create the object in question. Because the method is virtual and it's purpose is to create an object, it is nicknamed a "virtual constructor."

空袭的梦i 2024-11-22 06:11:46

C++ 中没有虚拟构造函数,但可以模拟行为

为什么 C++ 中没有虚拟构造函数?
我尝试给出推理:
该标准规定,直到构造函数的右大括号结束后,对象创建才完成。因此,对象仅在构造函数结束后才存在。

virtual 关键字用于实现多态行为,其中要调用的实际函数在运行时进行评估,具体取决于 this 所指向的对象的实际类型。为了使用虚表机制来调度构造函数,必须有一个完整存在的带有指向虚表指针的对象,但是在构造函数内部,对象构造本身并不完整,所以如何才能有指向虚表的指针如果物体没有完全形成就存在吗?

Bjarne Stroustrup 博士的推理:

为什么我们没有虚拟构造函数?

There are no virtual constructors in C++ though it is possible to simulate the behavior.

Why no virtual constructors in C++?
My attempt to give a Reasoning:
The standard states that the object creation is not complete until the closing brace of the constructor. Thus a object exists only after the constructor ends.

Virtual keyword is used to implement a polymorphic behavior, where in actual function to be called is evaluated at run time, depending on the actual type of object, this is pointing to. In order for the constructor to be dispatched using the virtual table mechanism, there has to be an completely existing object with a pointer to the virtual table, but inside a constructor the object construction itself is not complete so how can a pointer to the virtual table exist if the object isn't completely formed?

Reasoning of Dr. Bjarne Stroustrup:

Why don't we have virtual constructors?

番薯 2024-11-22 06:11:46

虚拟构造函数完全解释 因为虚拟构造函数或任何构造函数都会在之后自动调用对象的创建,或者我们可以说它是保证在对象生命周期中运行的第一个函数。当我们需要将基类指针绑定到派生类对象时,也需要虚函数,这是通过在运行时实现的后期绑定来完成的,但构造函数在编译时进行绑定以确认需要创建是否有默认构造函数。此外,对于后期绑定,需要虚拟指针,该指针不是在编译时创建的。

Virtual constructor Fully Explained As the virtual constructor or any constructor is called automatically just after the creation of object or we can say it is the guaranteed first function that will run in the life-cycle of the object. Also Virtual function is needed when we are in the need of binding the base class pointer to derived class object and this is done through late binding which is achieved at runtime but the constructor is bond at compile time to confirm that there is need of creation of default constructor or not. Also, to late binding there is need of virtual pointer which is not created at compile time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文