构造函数不能是虚拟的 [英] A constructor cannot be virtual

查看:252
本文介绍了构造函数不能是虚拟的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其中一个互联网C ++教程中,我发现了以下关于为什么构造函数不能是虚拟的


我们不能声明一个虚拟构造函数。我们应该在编译时指定对象的确切
类型,以便编译器可以为该特定类型分配
内存。


这个描述是否正确?



特别是对于,我会感到困惑,因此编译器可以分配$ b $ http://www2.research.att.com/~bs/bs_faq2.html#virtualceptctor\">在这里


虚拟调用是一种获取部分信息完成工作的机制。特别是,虚拟允许我们调用一个只知道一个接口而不是确切的对象类型的函数。要创建对象,您需要完整的信息。特别是,你需要知道你想要创建的确切类型。因此,对构造函数的调用不能是虚拟的。



In one of the C++ tutorials in internet, i found out the below description on why a constructor cannot be virtual

We cannot declare a virtual constructor. We should specify the exact type of the object at compile time, so that the compiler can allocate memory for that specific type.

Is this description correct ?

I am getting confused particularly with the phrase: so that the compiler can allocate memory for that specific type.

解决方案

As Bjarne himself explains here

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

这篇关于构造函数不能是虚拟的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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