gpt4 book ai didi

c++ - 初始化仅由一组对象共享的静态变量 C++

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:37 25 4
gpt4 key购买 nike

我想知道是否可以创建一个仅由同一类的一组对象共享的类数据成员。

我有一个名为 Scene 的类,还有一个名为 GameObject 的类,场景创建游戏对象,创建的每个游戏对象都必须引用它创建的场景。

我可以通过声明来实现这一点:

class GameObject
{
public:
Scene* scene;
}

并在场景创建游戏对象时放置。

void Scene::add_game_object(){
GameObject* gameobject = new GameObject();
gameobject->scene = this;
}

但是,这肯定会占用大量内存。

我在考虑一个解决方案,(目前还没有编译,但也许我们可以从中塑造一些东西)

class GameObject
{
public:
template< Scene* S >
Scene* get_scene();
}
//
template< Scene* S >
Scene* GameObject::get_scene(){
static Scene* sc = nullptr;
if( sc == nullptr ){
sc = S;
}
return sc;
}

void Scene::add_game_object(){
GameObject* gameobject = new GameObject();
// link scene and gameobject.
gameobject->get_scene(this);
}

使用它:

gameobject->get_scene<nullptr>();

谢谢。亲切地。

最佳答案

即使您有多个 Scene 对象,也可以将指向 Scene 对象的指针存储在 GameObject 的静态变量中。

想法是为每个 Scene 对象设置一个单独的类型,并为其 GameObject 设置一个单独的类型>。这允许为每个可以存储指针的 Scene 对象拥有一个单独的静态变量

下面的代码实现了这个想法。它要求在编译时设置Scenes 的最大数量,以便填充函数指针表:

#include <cassert>
#include <array>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

// forward declaration
template <std::size_t N>
struct Scene;

template <std::size_t N>
struct GameObject
{
GameObject()
{
std::cout << "created GameObject<" << N << "> which belongs to '" << scene->name << "'"<< std::endl;
}
static Scene<N>* scene;
};

template <std::size_t N>
Scene<N>* GameObject<N>::scene;

struct SceneBase
{
virtual ~SceneBase() = default;
virtual void addGameObject() = 0;
};

template <std::size_t N>
struct Scene : SceneBase
{
using GO = GameObject<N>;
Scene(const std::string& name) : SceneBase(), name(name)
{
GO::scene = this;
}

void addGameObject() override
{
gameObjects.push_back(std::make_unique<GO>());
}

std::vector<std::unique_ptr<GO>> gameObjects;
std::string name;
};


template <std::size_t N>
struct SceneMaker
{
static std::unique_ptr<SceneBase> make(const std::string& name)
{
std::cout << "making Scene<" << N <<"> with name '" << name << "'" << std::endl;
return std::make_unique<Scene<N>>(name);
}
};

using FunctionPtr = std::unique_ptr<SceneBase> (*)(const std::string&);

// based on http://stackoverflow.com/a/20408889/678093
template <std::size_t N, std::size_t... Rest>
struct FunctionTable
{
static constexpr auto& value = FunctionTable<N - 1, N, Rest...>::value;
};

template <std::size_t... Rest>
struct FunctionTable<0, Rest...>
{
static constexpr std::array<FunctionPtr,sizeof...(Rest)+1> value = {SceneMaker<0>::make,SceneMaker<Rest>::make...};
};

template <std::size_t... Rest>
constexpr std::array<FunctionPtr, sizeof...(Rest)+1> FunctionTable<0, Rest...>::value;


struct SceneFactory
{
auto makeScene(const std::string& name)
{
assert(sceneIndex < maxScenes);
return fTable[sceneIndex++](name);
}

static constexpr std::size_t maxScenes = 10;
static constexpr auto& fTable = FunctionTable<maxScenes>::value;

std::size_t sceneIndex = 0;
};


int main()
{
SceneFactory factory;
auto scene1 = factory.makeScene("first scene");
scene1->addGameObject();
scene1->addGameObject();
auto scene2 = factory.makeScene("second scene");
scene2->addGameObject();
scene2->addGameObject();
}

输出:

making Scene<0> with name 'first scene'
created GameObject<0> which belongs to 'first scene'
created GameObject<0> which belongs to 'first scene'
making Scene<1> with name 'second scene'
created GameObject<1> which belongs to 'second scene'
created GameObject<1> which belongs to 'second scene'

live example

关于c++ - 初始化仅由一组对象共享的静态变量 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069072/

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