为什么我们在C ++中没有虚拟构造函数? [英] Why do we not have a virtual constructor in C++?

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

问题描述

为什么C ++没有虚拟构造函数?

Why does C++ not have a virtual constructor?

推荐答案

来自Bjarne Stroustrup的C ++风格和技术常见问题
为什么我们没有虚拟构造函数?

From Bjarne Stroustrup's C++ Style and Technique FAQ Why don't we have virtual constructors?


虚拟调用是一种机制, $ b信息。特别是,虚拟允许我们仅仅知道任何接口而不知道对象的确切类型来调用函数
。要
创建一个对象,你需要完整的信息。特别是,你
需要知道你想要创建的确切类型。因此,
a对构造函数的调用不能是虚拟的。

A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only any 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.

FAQ条目继续给出代码方法来实现这个目的没有虚拟构造函数。

The FAQ entry goes on to give the code for a way to achieve this end without a virtual constructor.

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

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