小编And*_*ony的帖子

为什么我的对象似乎不使用`new`就在堆上?

我开始学习动态内存分配的主题。

我有以下代码:

#include <iostream>
#include "A.h"
#include "B.h"

using namespace std;

int main() {

   /* Both objects on Stack */

   A classAStack;
   B classBStack;

   /* Both objects on Heap*/
   //  A *classAHeap = new A();
   //  B *classBHeap = new B();

   /* A objects on Heap B ???*/
   A *classAHeap = new A();

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

#ifndef A_H_
#define A_H_

#include <iostream>
#include "B.h"

class A {
public:
   A();
   virtual ~A();

public:
   B b;
};

#endif /* A_H_ */
Run Code Online (Sandbox Code Playgroud)

#include …
Run Code Online (Sandbox Code Playgroud)

c++ heap-memory dynamic-memory-allocation stack-memory

3
推荐指数
1
解决办法
338
查看次数