class temp;
temp *t;
void foo() { temp foo2; t[1] = foo2; }
int main() {
t = new temp[100];
foo();
//t[1] is still in memory?
}
- 如果我想要一组这样的类,我是否必须使用
指向指针的指针?(并使用"new"
在数组中的每个元素上)例如:
temp **t;
- 如果我想做一个
100 ptr 到 ptr 的数组我必须做
temp **t = new temp[100][1];
是 没有更好的方法 4 个方括号?
最佳答案
代码:
t = new temp[100];
构造一个包含 100 个 temp 类型对象的数组。做同样事情的更安全的方法是:
std::vector <temp> t(100);
这使您不必再对数组调用 delete[]。
关于c++ - 类数组。栈还是堆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2296129/