简体   繁体   English

C++如何使用派生类构造函数销毁基类中的对象

[英]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:会做五件事:

  1. Call HangmanGraphics's constructor to create a temporary HangmanGraphics.调用 HangmanGraphics 的构造函数来创建一个临时的 HangmanGraphics。
  2. This results in a call to Hangman's constructor这导致调用 Hangman 的构造函数
  3. Call HangmanGraphics's = operator.调用 HangmanGraphics 的 = 运算符。 If one has not been specified, the default will be to copy the contents of the right hand HangmanGraphics into the left.如果未指定,则默认将右侧 HangmanGraphics 的内容复制到左侧。 It will copy a pointer, but not the pointed-to data.它将复制一个指针,但不会复制指向的数据。
  4. Call the destructor on the temporary HangmanGraphics在临时 HangmanGraphics 上调用析构函数
  5. Which results in a call to Hangman's destructor这导致调用 Hangman 的析构函数

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.

相关问题 C++ Unable to assign base class “this” pointer the derived class object inside base class constructor - C++ Unable to assign base class “this” pointer the derived class object inside base class constructor C++派生类构造函数调用基类构造函数错误 - C++ derived class constructor call base class constructor errors 在C ++中派生类构造函数之后调用基类构造函数 - Call base class constructor after the derived class constructor in C++ c++ 中派生的 class 构造函数中的动态基 class 构造函数调用 - dynamic base class constructor call in derived class constructor in c++ 如何调用所有基类的复制构造函数来复制C ++中钻石继承中的大多数派生类对象? - How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++? 如何在c ++中将指向基类的指针赋给派生类的对象? - How to assign a pointer to base class to an object of a derived class in c++? c++ - 如何通过基类引用访问派生类的对象? - How to access object of a derived class by base class reference in c++? 派生类的副本构造函数(C ++)的初始化列表上的基类 - Base class on the initialisation list of a derived class' copy constructor (C++) 基类作为C ++中派生类构造函数中的参数 - Base class as argument in derived class constructor in c++ C ++ - 实例化派生类并使用基类的构造函数 - C++ - Instantiating derived class and using base class's constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM