C++继承和类的新实例

C++ Inheritance and new instance of a class

本文关键字:实例 新实例 继承 C++      更新时间:2023-10-16

我有以下标题和类:

game.h:

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

GameStateManager.h

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

GameState.h

class GameState
{
    //bunch of lines here
}

PlayState.h

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

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

假设您使用的是头保护#pragma once,那么简单地说:

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