(C ++)如何在继承子类后删除基类?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(C ++)如何在继承子类后删除基类?相关的知识,希望对你有一定的参考价值。

我正在用C ++和SDL制作游戏。有'屏幕'课程。并且有“StartScreen”和“MultiplaySettingScreen”类,它们继承了“Screen”类。

我在控制台上发现,当我退出整个游戏时,这些类是按顺序构建的,但是为了解构,“Screen”对象不会被删除(释放),而“StartScreen”和“MultiplaySettingScreen”对象被删除(释放)。我认为这是内存泄漏。

屏幕构造函数()!

StartScreen构造函数()!

MultiplaySettingScreen构造函数()!

...

StartScreen构造函数()!

MultiplaySettingScreen构造函数()!

这是怎么回事?我做错了什么?


这是代码。

'屏幕'

class Screen
{
protected:
    SDL_Renderer *rend = nullptr;
    GameObject *screen = nullptr;
    GameObject *arrow = nullptr;
public:
    Screen();
    Screen(SDL_Renderer *renderer);
    virtual ~Screen(){};
    virtual void handleEvents(const Uint8 *keystate, bool *isSelecting, bool *isSingle, bool *isMulti){};
    virtual void Update(){};
    virtual void Render(){};
};

Screen::Screen()
{}

Screen::Screen(SDL_Renderer *renderer)
{
    std::cout << "Screen constructor()!" << std::endl;

    rend = renderer;
    screen = new GameObject("images/startscreen.png", GAME_WIDTH, GAME_HEIGHT, 0, 0, "img");
    arrow = new GameObject("images/arrow.png", 30, 30, 250, 330, "img");
}

“启动画面”

class StartScreen: public Screen
{
public:
    StartScreen(SDL_Renderer *renderer);
    ~StartScreen();
    virtual void handleEvents(const Uint8 *keystate, bool *isSelecting, bool *isSingle, bool *isMulti);
    virtual void Update();
    virtual void Render();
};

   StartScreen::StartScreen(SDL_Renderer *renderer): Screen(renderer)
    {
        std::cout << "StartScreen constructor()!" << std::endl;

        singlePlayMode = new GameObject("SINGLE PLAY", MODE_WIDTH, MODE_HEIGHT, 300, 320);
        multiPlayMode = new GameObject("MULTI PLAY", MODE_WIDTH, MODE_HEIGHT, 300, 380);
        title = new GameObject("Pikachu Volleyball", 400, MODE_HEIGHT, 200, 50);
        explaination = new GameObject("Press Enter on any mode..", 400, MODE_HEIGHT, 200, 200);
        copyright = new GameObject("(C) Jinko, All rights reserved", 400, 50, 200, 500);
    }

StartScreen::~StartScreen()
{
    std::cout << "StartScreen deconstructor()!" << std::endl;

    delete singlePlayMode;
    delete multiPlayMode;
    delete title;
    delete explaination;
    delete copyright;
    delete arrow;
    delete screen;
}

'MultiplaySettingScreen'

class MultiplaySettingScreen: public Screen
{
private:
    std::string connectingIp = "127.0.0.1";
    std::string connectingPort = "80";
public:
    MultiplaySettingScreen();
    ~MultiplaySettingScreen();
    virtual void handleEvents(const Uint8 *keystate, bool *isSelecting, bool *isSingle, bool *isMulti);
    virtual void Update();
    virtual void Render();
};

MultiplaySettingScreen::MultiplaySettingScreen()
{
    std::cout << "MultiplaySettingScreen constructor()!" << std::endl;

    host = new GameObject("Host", 400, 400, 200, 200);
    guest = new GameObject("Guest", 400, 400, 300, 200);
    ipInput = new GameObject("Connecting IP: ", 400, 200, 200, 200);
    portInput = new GameObject("Connecting PORT: ", 400, 50, 200, 500);
    SDL_StartTextInput();

    if (SDLNet_Init() == -1)
        std::cout << "SDLNET init failed" << std::endl;
    IPaddress ip;

}

MultiplaySettingScreen::~MultiplaySettingScreen()
{
    std::cout << "MultiplaySettingScreen deconstructor()!" << std::endl;

    SDLNet_Quit();

    delete host;
    delete guest;
    delete ipInput;
    delete portInput;
}

答案

这个计划

#include "StartScreen.h"
#include "MultiplaySettingScreen.h"

int main() {
    { StartScreen ss(nullptr); }
    { MultiplaySettingScreen mss; }
}

输出

(rendered) Screen constructor()!
StartScreen constructor()!
StartScreen deconstructor()!
Screen destructor()!
(default) Screen constructor()!
MultiplaySettingScreen constructor()!
MultiplaySettingScreen deconstructor()!
Screen destructor()!

我相信你缺少来自Screen课程的痕量输出来向你展示整个故事。

Screen.h

#ifndef SCREEN_H_INCLUDED
#define SCREEN_H_INCLUDED

#include <memory>
#include <iostream>

#include "stuff.h" // or whatever the SDL header(s) are called

class Screen
{
protected:
    SDL_Renderer *rend = nullptr;
    std::unique_ptr<GameObject> screen;
    std::unique_ptr<GameObject> arrow;
public:
    Screen();
    explicit Screen(SDL_Renderer *renderer);
    virtual ~Screen();
    Screen(Screen const&) = delete;
    Screen& operator=(Screen const&) = delete;

    virtual void handleEvents(const Uint8 *keystate, bool *isSelecting, bool *isSingle, bool *isMulti){}
    virtual void Update(){}
    virtual void Render(){}
};

Screen::Screen(SDL_Renderer *renderer)
: rend(renderer)
, screen(std::make_unique<GameObject>("images/startscreen.png", GAME_WIDTH, GAME_HEIGHT, 0, 0, "img"))
, arrow(std::make_unique<GameObject>("images/arrow.png", 30, 30, 250, 330, "img"))
{
    std::cout << "(rendered) Screen constructor()!
";
}

Screen::Screen() {
    std::cout << "(default) Screen constructor()!
";
}

Screen::~Screen() {
    std::cout << "Screen destructor()!
";
}

#endif  // SCREEN_H_INCLUDED

StartScreen.h

#ifndef STARTSCREEN_H
#define STARTSCREEN_H

#include "Screen.h"

class StartScreen : public Screen {
public:
    explicit StartScreen(SDL_Renderer *renderer);
    ~StartScreen() override;

    void handleEvents(const Uint8 *keystate, bool *isSelecting, bool *isSingle, bool *isMulti) override {}
    void Update() override {}
    void Render() override {}

private:
    std::unique_ptr<GameObject> singlePlayMode;
    std::unique_ptr<GameObject> multiPlayMode;
    std::unique_ptr<GameObject> title;
    std::unique_ptr<GameObject> explaination;
    std::unique_ptr<GameObject> copyright;
};

StartScreen::StartScreen(SDL_Renderer *renderer)
        : Screen(renderer),
          singlePlayMode(std::make_unique<GameObject>("SINGLE PLAY", MODE_WIDTH, MODE_HEIGHT, 300, 320)),
          multiPlayMode(std::make_unique<GameObject>("MULTI PLAY", MODE_WIDTH, MODE_HEIGHT, 300, 380)),
          title(std::make_unique<GameObject>("Pikachu Volleyball", 400, MODE_HEIGHT, 200, 50)),
          explaination(std::make_unique<GameObject>("Press Enter on any mode..", 400, MODE_HEIGHT, 200, 200)),
          copyright(std::make_unique<GameObject>("(C) Jinko, All rights reserved", 400, 50, 200, 500)) {
    std::cout << "StartScreen constructor()!
";
}

StartScreen::~StartScreen() {
    std::cout << "StartScreen deconstructor()!
";
}

#endif  // STARTSCREEN_H

MultiplaySettingScreen.h

#ifndef MULTIPLAYSETTINGSCREEN_H
#define MULTIPLAYSETTINGSCREEN_H

#include "Screen.h"

class MultiplaySettingScreen : public Screen {
private:
    std::string connectingIp = "127.0.0.1";
    std::string connectingPort = "80";
public:
    MultiplaySettingScreen();
    ~MultiplaySettingScreen() override;

    void handleEvents(const Uint8 *keystate, bool *isSelecting, bool *isSingle, bool *isMulti) override {}
    void Update() override {}
    void Render() override {}

    std::unique_ptr<GameObject> host;
    std::unique_ptr<GameObject> guest;
    std::unique_ptr<GameObject> ipInput;
    std::unique_ptr<GameObject> portInput;
};

MultiplaySettingScreen::MultiplaySettingScreen()
        : Screen(), host(std::make_unique<GameObject>("Host", 400, 400, 200, 200)),
          guest(std::make_unique<GameObject>("Guest", 400, 400, 300, 200)),
          ipInput(std::make_unique<GameObject>("Connecting IP: ", 400, 200, 200, 200)),
          portInput(std::make_unique<GameObject>("Connecting PORT: ", 400, 50, 200, 500)) {
    std::cout << "MultiplaySettingScreen constructor()!
";

    SDL_StartTextInput();

    if (SDLNet_Init() == -1)
        std::cout << "SDLNET init failed
";
    IPaddress ip;
}

MultiplaySettingScreen::~MultiplaySettingScreen() {
    std::cout << "MultiplaySettingScreen deconstructor()!
";
    SDLNet_Quit();
}

#endif  // MULTIPLAYSETTINGSCREEN_H
另一答案

“我在控制台上发现,当我退出整个游戏时,这些类按顺序构建,但是为了解构,'Screen'对象不会被删除(释放),而'StartScreen'和'MultiplaySettingScreen'对象被删除(释放)我认为这是一次内存泄漏。“

一个空的(默认)析构函数总是在超出范围或明确调用时销毁类...因此,如前所述,我们没有关于如何知道Screen没有被销毁的信息...

此外,这个成员:

SDL_Renderer *rend = nullptr;

SDL_Renderer是C-library SDL的struct部分,因此没有C ++ class-destructor ...因此,你的渲染器留下了对Screen类的破坏......

virtual ~Screen(){
    // Explicitly destroy the renderer...
    SDL_DestroyRenderer(rend);
    screen->~GameObject(); // Destroy the screen gameobject...
    arrow->~GameObject(); // Destroy the arrow gameobject...
};

以上是关于(C ++)如何在继承子类后删除基类?的主要内容,如果未能解决你的问题,请参考以下文章

继承基础

C# - 删除行后如何刷新 DataGridView

C++中如何在子类的构造函数中调用基类的构造函数来初始化基类成员变量

继承者们

如何在C#中模拟基类属性或方法

C++/MFC多重继承调用基类构造函数