为什么c++没有虚构造函数?
当前回答
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.
其他回答
c++中的虚函数是运行时多态性的一种实现,它们将执行函数覆盖。在c++中,当需要动态行为时,通常使用virtual关键字。只有当对象存在时,它才会工作。而构造函数用于创建对象。构造函数将在对象创建时调用。
因此,如果您将构造函数创建为virtual,根据virtual关键字定义,它应该有现有的对象可以使用,但构造函数用于创建对象,因此这种情况将永远不存在。所以你不应该将构造函数作为虚函数使用。
因此,如果我们试图声明虚拟构造函数编译器抛出一个错误:
构造函数不能声明为虚函数
当人们问这样的问题时,我喜欢对自己说:“如果这真的是可能的,会发生什么?”我真的不知道这意味着什么,但我猜这可能与能够基于所创建对象的动态类型重写构造函数实现有关。
我看到了一些潜在的问题。首先,在调用虚构造函数时,派生类不会被完全构造,因此实现中存在潜在的问题。
其次,在多重继承的情况下会发生什么?你的虚构造函数可能会被多次调用,然后你需要有某种方法知道哪个被调用了。
第三,一般来说,在构造时,对象并没有完全构造虚拟表,这意味着需要对语言规范进行很大的更改,以允许在构造时就知道对象的动态类型。这将允许基类构造函数在构造时调用其他虚函数,使用未完全构造的动态类类型。
最后,正如其他人指出的那样,您可以使用静态的“create”或“init”类型函数实现一种虚拟构造函数,基本上与虚拟构造函数所做的事情相同。
听可靠消息。:)
选自Bjarne Stroustrup的c++风格和技术常见问题解答为什么没有虚拟构造函数?
虚拟调用是一种在给定局部条件下完成工作的机制 信息。特别地,“virtual”允许我们调用一个函数 只知道任何接口,而不知道对象的确切类型。来 创建对象时需要完整的信息。特别是,你 需要知道你想要创建的确切类型。因此, “对构造函数的调用”不能是虚的。
FAQ条目继续给出了一种不使用虚拟构造函数实现此目的的方法的代码。
尽管由于对象类型是对象创建的先决条件,所以虚拟构造函数的概念不太适合,但它并没有完全被推翻。
GOF的“工厂方法”设计模式利用了虚拟构造函数的“概念”,这在某些设计情况下很方便。
在创建对象时创建Vpointer。创建对象前Vpointer不存在。因此,没有必要将构造函数设为虚函数。
推荐文章
- 为什么这个结合赋值和相等检查的if语句返回true?
- cplusplus.com给出的错误、误解或坏建议是什么?
- 找出质数最快的算法是什么?
- c++枚举类可以有方法吗?
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- 将析构函数设为私有有什么用?
- main()中的Return语句vs exit()
- 为什么c#不提供c++风格的'friend'关键字?
- 在函数的签名中添加关键字
- 我可以调用重载构造函数从另一个构造函数的同类在c# ?
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 为什么在标准容器中使用std::auto_ptr<>是错误的?
- 用比较double和0
- 保护可执行文件不受逆向工程的影响?
- 在c++中字符串前面的“L”是什么意思?