我大约6年前学过C++,我记得在大约阵列的部分,除非您使用新那么他们的大小是静态的,只能在字面常量,而不是在运行时的源代码中设置实例化他们。已静态数组的行为改变了吗?
但我只是玩弄的教程在
http://www.cplusplus.com/doc/tutorial/dynamic/
,并试图做没有新的,让我吃惊,它的工作。我误解了什么吗? 原代码是在提到URL,但它不是太难看是什么东西从下面我修改后的代码。
我实现与字符串,向量等...是不是真的需要阵列(或许可以解释为什么这个问题我从来就没想到),但只是幽默我:)
// rememb-o-matic
#include <iostream>
#include <new>
using namespace std;
int main()
{
int i,n;
// int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
int p[i];
// p= new (nothrow) int[i];
// if (p == 0)
// cout << "Error: memory could not be allocated";
if (false)
cout << "whut?" << endl;
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
// delete[] p;
}
return 0;
}
这是在C++中是非法的。你使用什么编译器? – 2012-02-18 11:55:53
i686的-苹果darwin11-LLVM-G ++ - 4.2 – scruffyDog 2012-02-18 11:56:59
如果没有,则应该使用任一'-std = C++ 98 -pedantic'或'-std =的C++ 0x -pedantic'用于您应该获得适当的警告。 – 2012-02-18 12:09:10