Why do we not have a virtual constructor in C++?(为什么我们在 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?
虚拟调用是一种在给定部分的情况下完成工作的机制信息.特别是,虚拟"允许我们调用一个函数只知道任何接口,而不知道对象的确切类型.到创建对象需要完整的信息.特别是你需要知道您要创建的确切类型.最后,对构造函数的调用"不能是虚拟的.
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++ 中没有虚拟构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我们在 C++ 中没有虚拟构造函数?
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
