为什么c++没有虚构造函数?
当前回答
虚函数基本上提供多态行为。也就是说,当您使用的对象的动态类型与引用它的静态(编译时)类型不同时,它提供的行为适合于对象的实际类型,而不是对象的静态类型。
现在尝试将这种行为应用于构造函数。当你构造一个对象时,静态类型总是与实际的对象类型相同,因为:
要构造一个对象,构造函数需要它要创建的对象的确切类型[…]此外[…]]则不能有指向构造函数的指针
Bjarne Stroustup (P424 c++编程语言SE)
其他回答
当人们问这样的问题时,我喜欢对自己说:“如果这真的是可能的,会发生什么?”我真的不知道这意味着什么,但我猜这可能与能够基于所创建对象的动态类型重写构造函数实现有关。
我看到了一些潜在的问题。首先,在调用虚构造函数时,派生类不会被完全构造,因此实现中存在潜在的问题。
其次,在多重继承的情况下会发生什么?你的虚构造函数可能会被多次调用,然后你需要有某种方法知道哪个被调用了。
第三,一般来说,在构造时,对象并没有完全构造虚拟表,这意味着需要对语言规范进行很大的更改,以允许在构造时就知道对象的动态类型。这将允许基类构造函数在构造时调用其他虚函数,使用未完全构造的动态类类型。
最后,正如其他人指出的那样,您可以使用静态的“create”或“init”类型函数实现一种虚拟构造函数,基本上与虚拟构造函数所做的事情相同。
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
听可靠消息。:)
选自Bjarne Stroustrup的c++风格和技术常见问题解答为什么没有虚拟构造函数?
虚拟调用是一种在给定局部条件下完成工作的机制 信息。特别地,“virtual”允许我们调用一个函数 只知道任何接口,而不知道对象的确切类型。来 创建对象时需要完整的信息。特别是,你 需要知道你想要创建的确切类型。因此, “对构造函数的调用”不能是虚的。
FAQ条目继续给出了一种不使用虚拟构造函数实现此目的的方法的代码。
If you think logically about how constructors work and what the meaning/usage of a virtual function is in C++ then you will realise that a virtual constructor would be meaningless in C++. Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class, however the constructor is called when the objected is created, at that time you cannot be creating a sub-class of the class, you must be creating the class so there would never be any need to declare a constructor virtual.
另一个原因是,构造函数的名字与其类名相同如果我们将构造函数声明为virtual,那么它应该在它的派生类中以相同的名字重新定义,但两个类的名字不能相同。所以不可能有一个虚拟构造函数。
虚函数基本上提供多态行为。也就是说,当您使用的对象的动态类型与引用它的静态(编译时)类型不同时,它提供的行为适合于对象的实际类型,而不是对象的静态类型。
现在尝试将这种行为应用于构造函数。当你构造一个对象时,静态类型总是与实际的对象类型相同,因为:
要构造一个对象,构造函数需要它要创建的对象的确切类型[…]此外[…]]则不能有指向构造函数的指针
Bjarne Stroustup (P424 c++编程语言SE)
推荐文章
- 为什么这个结合赋值和相等检查的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”是什么意思?