我正在开发一个实体组件系统,并尝试根据从Component类本身派生的类数来创建Component类型编号。
但我认为C ++中有一些缺少的功能可以满足我的需要。 因为Component类的数量应该是一些constexpr整数,我应该用来分隔vector,bitmask等... 现在我可以让每个派生类都有一个唯一的类型号,但是不能检测bitset的大小,即派生类的数量。
基地:
//!
//! \class ComponentBase
//! \brief Exist only to manage CounterType in a prepocessor way
//!
class ComponentBase {
protected:
static uint32_t CounterType; // Counter of actual component number
public:
virtual ~ComponentBase() {}
};
}
typedef std::bitset<ComponentBase::CounterType> T_Mask;
派生:
//!
//! \class Component
//! \brief Superclass for Component, stock Type number and Manager
//!
template < typename Derived >
class Component : public ComponentBase {
public:
static const uint32_t Type;
protected:
Component() = default;
};
}
template < typename Derived >
const uint32_t Component<Derived>::Type = ++ComponentBase::CounterType;
但是现在我不能使用CounterType来设置bitset大小。 尝试使用constexpr但没有任何成功。
如果你有一些想法,我会全力以赴。 不管怎样,谢谢
Ps:我没有任何C ++限制(现在是G ++ 6-2)
答案 0 :(得分:1)
在c ++中无法满足您的要求。
假设您的Base和Derived通过header.hpp
可用(传递#include)并且是合法的c ++
项目中包含以下文件:
1.cpp
#include "header.hpp"
class One {};
class ComponentOne : public Component<One> {};
2.cpp
#include "header.hpp"
class Two {};
class ComponentOne : public Component<Two> {};
你旋转cc.exe一次将1.cpp编译成1.o,另一个实例将2.cpp编译成2.o,会发生什么?
答案 1 :(得分:0)
很抱歉,但你必须决定:
std::bitset<size_t N>
需要N
的const(expr)数字(某事),以便您可以通过
static const uint32_t CounterType = 1; // Counter of actual component number
但在这种情况下:++ComponentBase::CounterType;
将无效,因为您尝试增加常量变量。
我觉得你的设计有些可疑,所以请在我们的场景信息背后分享更多信息,以便更清楚地识别问题。