gpt4 book ai didi

c++ - 由于继承而返回拷贝的堆栈?

转载 作者:行者123 更新时间:2023-11-28 05:32:17 25 4
gpt4 key购买 nike

我在尝试让我的堆栈容器适配器通过引用从 getter 返回时遇到了一些麻烦。每次我尝试对它进行吸气时,即使使用引用运算符,它似乎也在复制它。我这样说是因为每当我在一个类中调试和观察容器时,容器的大小如预期的那样为 1,因为我将一个对象推送到堆栈。但是,一旦我通过函数进入另一个类...该类将堆栈的大小更改回 0。直到我返回到调用该函数的内容,然后它返回到 1。

是因为我继承自使用容器的类吗?接下来是我的下一个问题......继承是复制母类中的成员还是引用它们?我将尝试用最少的代码来解释这一点,所以请保持友好...

GAME.HPP

#ifndef GAME_HPP
#define GAME_HPP

#include <stack>
#include "GameState.hpp"

class Game
{
public:
Game();
~Game();

int playGame();

protected:
std::stack<GameState*>& getGStates();

private:
std::stack<GameState*> gameStates; //GameState is a abstract class (no implementation)
bool gameRunning;
};

#endif

GAME.CPP

int Game::playGame()
{
gameRunning = true;

SplashScreen sScreen; // SplashScreen inherits from GameState class
gameStates.push(std::move(&sScreen)); //move screen to stack without making a copy

// The stack size is now 1

while (gameRunning)
{
gameStates.top()->runState();
}
return 0;
}

std::stack<GameState*>& Game::getGStates()
{
return gameStates; //returns stack
}

GAMESTATE.HPP

#ifndef GAMESTATE_HPP
#define GAMESTATE_HPP

class GameState
{
public:
GameState(){};
~GameState(){};

virtual void runState() = 0;
virtual void resumeState() = 0;
virtual void pauseState() = 0;
virtual void update() = 0;
virtual void render() = 0;
};
#endif

我确实在 SplashScreen 类中提供了大量的实现,但是如果我删除它,问题仍然存在。所以现在为了简单起见,我只想说它实际上与您在 GameState header 中看到的没有什么不同,只是有空白 block 和返回。我仍将显示 runState() 位,但将其简化以确定问题的范围。

SPLASHSCREEN.CPP

//obvious header includes above

void SplashScreen::runState()
{
std::cout << getGStates() << std::endl; //here the stack is 0
std::cin.get(); //getting char from input stream so program won't terminate so fast
return; //here I return to what invoke the runState() but stack becomes 1 again in the other class...Is a copy being returned by the getter ??
}

不过,我想提及的一件事是 SplashScreen 类确实继承自 GameStateGame 类。我继承自 GameState 类,因此我可以将它用作我的 SplashScreen 类的接口(interface),并且我继承自 Game 类,因此我可以使用 setter/getter 方法。

您可能会说为什么我不通过 Game.cpp 中的函数 runState() 来传递引用。我可以而且它会工作,但是有没有另一种方法可以做到这一点而不用参数弄乱我的函数?我不想每次需要时都添加参数。再说一次,如果我这样做,我的对象设计很差,对吗?请帮助...

最佳答案

您提到 SplashScreen 继承自 Game。因此,如下代码:

void SplashScreen::runState()
{
std::cout << getGStates() << std::endl; //here the stack is 0
std::cin.get(); //getting char from input stream so program won't terminate so fast
return; //here I return to what invoke the runState() but stack becomes 1 again in the other class...Is a copy being returned by the getter ??
}

打印 Splashscreen 实例对象的堆栈,而不是初始的 Game 实例对象。换句话说,您打印的堆栈与您将 SplashScreen 对象推送到的堆栈不同,甚至不是该对象的拷贝,而是一个完全不相关的 GameState 堆栈。

您的问题是:继承不是推断 GameSplashScreen 之间关系的正确方法。继承主要是模型“是一种”类之间的关系,看起来 SplashScreen 可能不是一种游戏。您想要表示的这种关系是一种使用关系,通过引用您将要使用的对象或将其作为参数传递到调用堆栈中的某处来表示。

如果您需要访问游戏,您可能应该:

  1. 将其作为成员变量添加到您的 SplashScreen 类中,例如

    //Splashscreen.h
    class SplashScreen : public GameState {
    public:
    SplashScreen(Game& game)
    : m_game(game) {
    }

    ...

    private:
    Game& m_game;
    }

    //SplashScreen.cpp
    void SplashScreen::runState()
    {
    std::cout << m_game.getGStates() << std::endl; //here the stack is 0
    std::cin.get(); //getting char from input stream so program won't terminate so fast
    return; //here I return to what invoke the runState() but stack becomes 1 again in the other class...Is a copy being returned by the getter ??
    }
  2. 或将其作为参数传递给您的 runState 方法,例如

    void SplashScreen::runState(Game& game)
    {
    std::cout << game.getGStates() << std::endl; //here the stack is 0
    std::cin.get(); //getting char from input stream so program won't terminate so fast
    return; //here I return to what invoke the runState() but stack becomes 1 again in the other class...Is a copy being returned by the getter ??
    }

关于c++ - 由于继承而返回拷贝的堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com