- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试让我的堆栈容器适配器通过引用从 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
类确实继承自 GameState
和 Game
类。我继承自 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
堆栈。
您的问题是:继承不是推断 Game
和 SplashScreen
之间关系的正确方法。继承主要是模型“是一种”类之间的关系,看起来 SplashScreen 可能不是一种游戏。您想要表示的这种关系是一种使用关系,通过引用您将要使用的对象或将其作为参数传递到调用堆栈中的某处来表示。
如果您需要访问游戏,您可能应该:
将其作为成员变量添加到您的 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 ??
}
或将其作为参数传递给您的 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/
我有一个基类和两个派生类,我需要将一个指向派生类对象的指针复制到另一个类中,就像示例一样。 class Base { public: Base(const Base& other); } cl
考虑 Container 类,它主要存储 Box 对象的 unique_ptr vector ,并可以对它们执行一些计算。 class Container { private: std::
引用是指保存的值为对象的地址。在 Python 语言中,一个变量保存的值除了基本类型保存的是值外,其它都是引用,因此对于它们的使用就需要小心一些。下面举个例子: 问题描述:已知一个列表,求生成一个
我正在尝试实现 Bron-Kerbosch 算法,这是一种用于查找派系的递归算法。我设法达到了一个点,它返回了正确数量的派系,但是当我打印它们时,它们不正确 - 添加了额外的节点。我在这里遗漏了什么明
在评估中,我选择了选项LINE I 上的运行时错误。没有未定义行为这样的选项,尽管我认为这是正确的选择。 我不确定,但我认为评估有误。我编译并运行了该程序,它确实打印了 3, 9, 0, 2, 1,
在函数签名中通过 const 值传递参数是否有任何好处(或相反,成本)? 所以: void foo( size_t nValue ) { // ... 对比 void foo( const s
我为 answer to another question 写了一个 OutputIterator .在这里: #include using namespace std; template clas
我有一个由第三方生成的 dll,它具有某种内部数据结构,将其大小限制为 X 个元素。 所以基本上,它有一个以 X 为限制的队列。 据我所知,DLL 是每个进程的,但是是否可以多次加载 DLL?也许每个
假设我有以下两个数据结构: std::vector all_items; std::set bad_items; all_items vector 包含所有已知项和 bad_items vector
如何在不渲染 CGImage 的情况下从另一个 CIImage 复制一个 CIImage 最佳答案 CIImage *copiedImage = [originalImage copy]; 正如您在
我有一个名为 UINode 的 GUI,我想创建一个拷贝并只更改一些内容。该项目由 3 个基本线程组成。 PingThread、RosThread 和 GuiThread。我试图复制粘贴项目文件夹并将
Qt 新手。如果这个问题太幼稚,请多多包涵。在 Windows 操作系统环境中,我有 Qt 对话框框架应用程序,它具有“重复”- 按钮。在同一目录中,有 Qt 应用程序 - (一个带有关闭按钮的对话框
我正在尝试创建一个函数来复制我的卡片结构。我只需复制 cvalue 即可轻松开始。然而,我的 cvalue 没有复制,当应该读取 1000 时它仍然读取 5。 #include #include
string str1("someString"); string str2 = string(str1);//how many copies are made here //copy2 =
我希望了解 boost::bind 执行何种函数对象的内部拷贝。由于这些对象的构造函数似乎没有被调用,我推测这是一种“非常浅的复制”,所以我引入了动态内存分配来产生一些错误。但是,下面代码的运行时输出
我正在查看 http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c22-make-default-operations-consis
下面的类方法Augmented3dPoint::getWorldPoint()返回对其成员的引用 cv::Point3f world_point; class Augmented3dPoint { p
我需要通过 MyClass2 将用户定义的 lambda 传递给 MyClass1。我想确保只有一步,没有拷贝。下面的代码实现了吗?有没有更好的方法来做到这一点(比如使用编译器完成的隐式移动)? 注意
在我的数据库访问代码中,我想写一个方法: variant_t GetQueryRows (...) 我想这样调用它: const variant_t result = GetQueryRows (..
我有一个包含引用的类,例如: class A { A(B &b) : b(b) {} // constructor B &b; } 有时b必须是只读的,有时是可写的。当我创建一个 const A
我是一名优秀的程序员,十分优秀!