【发布时间】:2013-11-18 03:16:53
【问题描述】:
我有一个游戏类、立方体类和一个解决方案类。在我的主要功能中,我正在创建一个游戏对象 g 并且该游戏对象具有一组立方体类型对象和一堆解决方案类型对象。在 main 函数中,在退出之前,我通过调用 delete[] g; 来删除对象 g;
我想知道这是否会删除数组和堆栈中的所有对象?或者我还会有内存泄漏吗?
示例代码:
int main(void)
{
Game* g = new Game();
//Do something like addCube by calling functions in game
delete g;
}
游戏构造器:
public: Game()
{
int nx;
cout<<"Enter the number of cubes in the game ";
cin>>nx;
this->numOfCubes=nx;
cubes = new Cube[nx];
this->ref=0;
}
游戏功能示例
void Game::addCube()
{
if(ref<numOfCubes)
{
cubes[ref].getSides();
ref++;
cubes[ref]->Sno = ref;
}
else
cout<<"All the cubes are already in the game"<<endl;
}
【问题讨论】:
-
我认为您需要提供一些示例代码,以便我们知道您真正在说什么。 sscce.org
-
@FredLarson:添加代码