为什么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.
其他回答
我能想到两个原因:
技术原因
对象只有在构造函数结束后才存在。为了使用虚拟表分派构造函数,必须有一个现有的对象和指向虚拟表的指针,但是如果对象仍然不存在,指向虚拟表的指针怎么可能存在呢?:)
逻辑的原因
当您想要声明某种多态行为时,可以使用virtual关键字。但是构造函数没有任何多态性,c++中的构造函数只是简单地将对象数据放到内存中。由于虚表(以及一般的多态性)都是关于多态行为而不是多态数据的,因此声明虚构造函数没有任何意义。
虚函数基本上提供多态行为。也就是说,当您使用的对象的动态类型与引用它的静态(编译时)类型不同时,它提供的行为适合于对象的实际类型,而不是对象的静态类型。
现在尝试将这种行为应用于构造函数。当你构造一个对象时,静态类型总是与实际的对象类型相同,因为:
要构造一个对象,构造函数需要它要创建的对象的确切类型[…]此外[…]]则不能有指向构造函数的指针
Bjarne Stroustup (P424 c++编程语言SE)
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关键字定义,它应该有现有的对象可以使用,但构造函数用于创建对象,因此这种情况将永远不存在。所以你不应该将构造函数作为虚函数使用。
因此,如果我们试图声明虚拟构造函数编译器抛出一个错误:
构造函数不能声明为虚函数
我们不能简单地说…我们不能继承构造函数。因此没有必要将它们声明为虚拟的,因为虚拟提供了多态性。
推荐文章
- 未定义对静态constexpr char的引用[]
- 在c++中,restrict关键字是什么意思?
- c++中类似于java的instanceof
- include_directories和target_include_directories在CMake中的区别是什么?
- std::make_pair与std::pair的构造函数的目的是什么?
- 如何追加一个字符到std::字符串?
- 为什么要在c++中使用嵌套类?
- 如何处理11000行c++源文件?
- 使用g++编译多个.cpp和.h文件
- 如何在c++中追加文本到文本文件?
- 在c++中使用"super
- Mmap () vs.读取块
- 什么是不归路?
- 将类代码分离为头文件和cpp文件
- 不能在双精度上使用模量?