将对象初始化为子类C ++的实例

时间:2021-08-28 16:53:43

I wasn't sure how to word what I'm looking for, which is probably why I can't find an answer by searching. Basically, I've got a class with another object as a member, and I want to initialize that member as an instance that is a subclass of that members type. The encapsulating class is called Game, and the member variable is of classtype GameState, and I would like to initialize it as a new instance of MainMenuGameState, which extends GameState.

我不确定如何说出我正在寻找的东西,这可能就是为什么我无法通过搜索找到答案。基本上,我有一个类与另一个对象作为成员,我想将该成员初始化为一个实例,该实例是该成员类型的子类。封装类称为Game,成员变量是classtype GameState,我想将它初始化为MainMenuGameState的新实例,它扩展了GameState。

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

class GameState {}; //Will be abstract class, not to be instantiated

class Game {
    private:
        GameState gameState; //Would want this to be initialized as a MainMenuGameState, not just a GameState;
    public:
        Game();
}

I can think of how to do this in Java, but not in C++. Thanks!

我可以想一想如何在Java中实现这一点,而不是在C ++中。谢谢!

3 个解决方案

#1


One of the many fundamental language differences between Java and C++ is that all Java objects are implicitly referenced through shared pointers.

Java和C ++之间的许多基本语言差异之一是所有Java对象都是通过共享指针隐式引用的。

C++ gives you the choice (and therefore the burden) of expressing whether objects should be managed by the scope of pointers to them, or by the scope of the object itself.

C ++为您提供了选择(因而也是负担),表达对象是应该通过指向它们的指针范围来管理,还是通过对象本身的范围来管理。

In order to hold any polymorphic object you must hold it through some means of pointer (as the the other answers mention, best to use unique_ptr or shared_ptr depending on whether the object's lifetime is to be managed by one owner or many).

为了保存任何多态对象,你必须通过一些指针来保持它(正如其他答案所提到的,最好使用unique_ptr或shared_ptr,具体取决于对象的生命周期是由一个所有者还是多个来管理)。

full example (warnings fixed):

完整示例(警告已修复):

#include <memory>

class GameState {
public:
    virtual ~GameState() = default;
}; //Will be abstract class, not to be instantiated

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

class Game {
    private:
        std::unique_ptr<GameState> gameState; //Would want this to be initialized as a MainMenuGameState, not just a GameState;
    public:
        Game();
};

Game::Game()
: gameState { new MainMenuGameState }
{}


using namespace std;

int main()
{
    Game g;

   return 0;
}

#2


Use a pointer, preferably std::unique_ptr:

使用指针,最好是std :: unique_ptr:

#include <memory>

class Game {
private:
    std::unique_ptr<GameState> gameState;
public:
    Game()
    : gameState(std::unique<MainMenuGameState>(new MainMenuGameState())) { }
};

#3


Use a pointer to the base class: GameState* pGameState. And then in your constructor:

使用指向基类的指针:GameState * pGameState。然后在你的构造函数中:

MainMenuGameState::MainMenuGameState() :
    pGameState(new MainMenuGameState())
{
}

MainMenuGameState::~MainMenuGameState()
{
    delete pGameState;
}

And you should add a virtual destructor to GameState so it can properly clean up any resources:

您应该向GameState添加一个虚拟析构函数,以便它可以正确地清理任何资源:

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

#1


One of the many fundamental language differences between Java and C++ is that all Java objects are implicitly referenced through shared pointers.

Java和C ++之间的许多基本语言差异之一是所有Java对象都是通过共享指针隐式引用的。

C++ gives you the choice (and therefore the burden) of expressing whether objects should be managed by the scope of pointers to them, or by the scope of the object itself.

C ++为您提供了选择(因而也是负担),表达对象是应该通过指向它们的指针范围来管理,还是通过对象本身的范围来管理。

In order to hold any polymorphic object you must hold it through some means of pointer (as the the other answers mention, best to use unique_ptr or shared_ptr depending on whether the object's lifetime is to be managed by one owner or many).

为了保存任何多态对象,你必须通过一些指针来保持它(正如其他答案所提到的,最好使用unique_ptr或shared_ptr,具体取决于对象的生命周期是由一个所有者还是多个来管理)。

full example (warnings fixed):

完整示例(警告已修复):

#include <memory>

class GameState {
public:
    virtual ~GameState() = default;
}; //Will be abstract class, not to be instantiated

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

class Game {
    private:
        std::unique_ptr<GameState> gameState; //Would want this to be initialized as a MainMenuGameState, not just a GameState;
    public:
        Game();
};

Game::Game()
: gameState { new MainMenuGameState }
{}


using namespace std;

int main()
{
    Game g;

   return 0;
}

#2


Use a pointer, preferably std::unique_ptr:

使用指针,最好是std :: unique_ptr:

#include <memory>

class Game {
private:
    std::unique_ptr<GameState> gameState;
public:
    Game()
    : gameState(std::unique<MainMenuGameState>(new MainMenuGameState())) { }
};

#3


Use a pointer to the base class: GameState* pGameState. And then in your constructor:

使用指向基类的指针:GameState * pGameState。然后在你的构造函数中:

MainMenuGameState::MainMenuGameState() :
    pGameState(new MainMenuGameState())
{
}

MainMenuGameState::~MainMenuGameState()
{
    delete pGameState;
}

And you should add a virtual destructor to GameState so it can properly clean up any resources:

您应该向GameState添加一个虚拟析构函数,以便它可以正确地清理任何资源:

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