我想知道是否可以创建仅由同一个类的一组对象共享的类数据成员。
我有一个名为 Scene 的类,另一个名为 GameObject , 场景创建GameObjects,并且创建的每个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>();
谢谢。 亲切。
答案 0 :(得分:3)
即使您有多个Scene
对象,也可以将GameObject
对象的指针存储在Scene
的静态变量中。
我们的想法是为每个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'