c++ - 在堆栈或堆上创建的类成员?

标签 c++ class stack heap

需要知道是否会在堆栈中或堆上创建这样的3d矩阵,以及是否在堆栈上创建新的3d矩阵并正确初始化默认值(内存集)

class Matrix {
     protected:
         int n[9000][420]; // is stack or heap if VVV is pointer?
};

void main()
{
         Matrix* t = new Matrix(); // created on heap
}

最佳答案

一切都取决于您如何创建父对象。

MyClass {
    int n[10];
};

int main(...) {
    MyClass *c1 = new MyClass; // everything allocated on heap, 
                               // but c1 itself is automatic
    MyClass c2;                // everything allocated on stack
    // ...
}

堆和堆栈当然是实现细节,但是在这种情况下,我认为可以指定。

关于c++ - 在堆栈或堆上创建的类成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9073022/

相关文章:

class - python 3 中的 types.ClassType 发生了什么?

java - 我已经设法编译了 java 程序,但我无法执行它

c++ - 分配原始类型后删除 void 指针

c++ - 类模板,如果对象是X类型的成员函数定义?

java - 尽管不允许多重继承,但一个类如何扩展另一个类,扩展 Object 类?

c++ - 阿桑 : heap-use-after-free when flattening a binary tree to a linked list

android - 从堆栈中删除 Activity

c - 程序中途终止

c++ - 如何将外部音频流转发到虚拟音频电缆?

c++ - usleep 是否创建线程取消点?