C++ 类的继承和新实例

标签 c++ inheritance

<分区>

我有以下标题和类:

游戏.h:

#include "GameStateManager.h"
class game
{
    //bunch of lines here
}

游戏状态管理器.h

#include "GameState.h"
class GameStateManager
{
    //bunch of lines here
}

游戏状态.h

class GameState
{
    //bunch of lines here
}

播放状态.h

#include "GameState.h"
class PlayState : public GameState
{
    //bunch of lines here
}

我需要在“game.cpp”中创建一个新的 PlayState 实例。我该怎么做?

最佳答案

假设您正在使用 header guards#pragma once,只需:

#include "GameStateManager.h"
#include "PlayState.h"
class game
{
    PlayState play_state;
    //bunch of lines here
}

关于C++ 类的继承和新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26168931/

相关文章:

c++ - 使用 libtool 从共享库加载重复的函数名

c++ - 正确使用boost Lockguard——这两个是一样的吗

c++ - "Locking"两个 vector 并对它们进行排序

c++ - 快速定位MSCV 2010中的错误信息

css - 防止 anchor 标签继承CSS样式

c++ - 获取像流字符串这样的函数参数

angular - 如何将注入(inject)服务从父类(super class)继承到 Angular 2 中的子类组件

java - java接口(interface)与继承

inheritance - 使用 Typescript 在 Angular 2 中的组件之间共享行为

c++ - 如何使用具有特定方法的类实例化 C++ 模板