fyo*_*iev 6 c++ constructor new-operator
偶尔,我在我的程序代码的一个地方写了一个拼写错误:
int a = 10;
char* b = new char(a);
Run Code Online (Sandbox Code Playgroud)
错误是显而易见的:我写了()而不是[].奇怪的是...代码编译好了,它在调试器中运行确定.但是在执行这些行的函数之后,在调试器之外编译的.exe崩溃了一会儿.
第二行代码真的合法吗?如果是,那对编译器意味着什么?
您正在分配单个 char并为其分配值a.它根本没有分配数组.
它与在new任何其他类型的表达式中调用构造函数相同:
std::string* s = new std::string("foo");
int* i = new int(10);
std::vector<std::string>* v = new std::vector<std::string>(5, "foo");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1173 次 |
| 最近记录: |