[英]C++ how to destroy an object in the base class with a derived class constructor
So in my program I have a derived class called hangman and a derived class called HangmanGraphics.因此,在我的程序中,我有一个名为 hangman 的派生类和一个名为 HangmanGraphics 的派生类。 The issue I'm having is after the game I want to reset my variables but for some reason my base constructor does not get restarted or even called.
我遇到的问题是在游戏结束后我想重置我的变量,但由于某种原因,我的基本构造函数没有重新启动甚至没有被调用。 When I run the program it will go to the base constructor once but towards the end of the program when:
当我运行程序时,它将转到基本构造函数一次,但在程序结束时:
#include "player.h"
#include "hangman.h"
#include "HangmanGraphics.h"
using namespace std;
int main()
{
HangmanGraphics game2;
while(1)
{
game2.Play();
if(game2.Play()=='Y')
{
game2=HangmanGraphics();
//in this part of the code I want to reset my base
//constructor values but how do I do that by using the
//derived construtor
continue;
}
}
}
Cheap hack.便宜的黑客。 Moving the definition of game2 into the while loop will provide a new game2 every pass though the loop:
将 game2 的定义移动到 while 循环中将在每次通过循环时提供一个新的 game2:
int main()
{
while(1)
{
HangmanGraphics game2; // game2 created here
// do stuff
} // game2 destroyed here
}
There is not enough information provided in your question to say whether this is a good thing to do or a bad.您的问题中没有提供足够的信息来说明这是一件好事还是一件坏事。 But...
但...
game2=HangmanGraphics();
will do five things:会做五件事:
If you have any dynamically allocated storage in HangmanGraphics or Hangman, you need to read up on the Rule of Three如果您在 HangmanGraphics 或 Hangman 中有任何动态分配的存储空间,则需要阅读三法则
Anything dynamically allocated within the temporary HangmanGraphics and it's base Hangman will be (or should have been) deleted by the destructor.在临时 HangmanGraphics 及其基础 Hangman 中动态分配的任何内容都将(或应该)被析构函数删除。 If a operator= was not custom-defined to handle the dynamic data, game2 now contains pointers to invalid storage.
如果 operator= 不是自定义定义来处理动态数据,那么 game2 现在包含指向无效存储的指针。
Example:例子:
class base
{
public:
base(): baseval(number++), interestingData(new int(number++))
{
cout << "base ctor " << baseval << "," << * interestingData <<endl;
}
virtual ~base()
{
cout << "base dtor " << baseval << "," << * interestingData <<endl;
delete interestingData;
}
private:
int baseval;
int * interestingData;
};
class derived: public base
{
public:
derived():base()
{
cout << "derived ctor" <<endl;
}
virtual ~derived()
{
cout << "derived dtor" <<endl;
}
derived & operator=(const derived & rhs) //only exists to do the cout
{
cout << "derived =" <<endl;
base::operator=(rhs);
return *this;
}
};
int main()
{
derived test;
cout << "created test" << endl;
test = derived();
cout << "exit" << endl;
}
Output输出
base ctor 0,1
derived ctor
created test
base ctor 2,3
derived ctor
derived =
derived dtor
base dtor 2,3
exit
derived dtor
base dtor 2,4067440
interestingdata in the last print is smashed because the address it points to has been reassigned.最后一次打印中的有趣数据被粉碎,因为它指向的地址已被重新分配。 Even more fun, it is about to be deleted a second time and that should crash your program.
更有趣的是,它即将被第二次删除,这将使您的程序崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.