class Levels{
public:
Levels();
Player* one;
Player* two;
Player* three;
Player* four;
void reset();
}
Levels::Levels()
{
one = new Player(...);
two= new Player(...);
three= new Player(...);
four= new Player(...);
}
void Levels::reset()
{
delete one;
delete two;
delete three;
delete four;
one = new Player(...);
two= new Player(...);
three= new Player(...);
four= new Player(...);
}
基本上在重置时它会删除旧玩家并使它们再次成为新玩家。它有效,但我不知道它是否可行。
答案 0 :(得分:1)
您发布的代码不会泄漏内存。但如果复制Levels会发生什么?没有复制构造函数等就会泄漏。它是否有析构函数?
为什么不使用智能指针,例如std::unique_ptr<Player>
。这将在重置或销毁时自动删除旧玩家。