c++ - 类数组。栈还是堆?

标签 c++ arrays pointers

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/

相关文章:

c++ - 编译器无法将指向字符串文字的指针解释为方法的参数

C++ vector <整数> : add int elements to vector that are even and already in the vector

c++ - "Recursive Makefile Considered Harmful"样式 makefile 问题

c++ - 运算符 << 在构造函数中重载

arrays - 当数组不超过 50 个元素时,在 PostgreSql 中使用表而不是数组字段类型更好吗?

c++ - C++ 中的引用及其内存要求

c++ - Xcode 不打印日志/控制台消息。显示黑色调试器

arrays - 索引超出范围 uitableview in swift 3

javascript - 从获取的 WMS GetCapability 请求返回数组

C链表指针问题