琐碎的C ++ code ......为什么会这样编? [英] Trivial C++ code... Why does this compile?

查看:130
本文介绍了琐碎的C ++ code ......为什么会这样编?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++教程如这个说,所有数组的大小必须在事先程序正在运行的决定。例如,这是不允许的:

C++ tutorials like this say that the size of all arrays must be decided upon in advance of the program being run. For example, this is not allowed:

cout << "How many variables do you want? ";
int nVars;
cin >> nVars;

int anArray[nVars]; // wrong!  The size of the array must be a constant

但这种琐碎的程序的确实的编译和执行罚款。的应该的吗?

But this trivial program does compile and execute fine. Should it?

推荐答案

这是C ++编译器,如GNU的G ++实现的一个常见的​​扩展。与 -std =的C ++ 0x 编译标志来对待这样的声明是一个错误。

This is a common extension implemented by C++ compilers, such as GNU's g++. Compile with -std=c++0x flag to treat such declaration as an error.

这篇关于琐碎的C ++ code ......为什么会这样编?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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