我正在开发一个实体组件系统,并试图根据组件类本身派生出多少类来确定组件类型编号。
但是我认为在C++中有一些我想做的事情缺少的特性。因为组件类的数量应该是一个常数整数,我应该用它来分隔向量、位掩码等.现在,我可以让每个派生类都有一个唯一的类型编号,但不能检测到位集的大小,即派生类的数量。
基地:
//!
//! \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来设置位集大小。和警察一起试过但没有成功。
如果你有什么主意的话,我会洗耳恭听的。无论如何,谢谢
Ps:我没有任何C++限制(G++ 6-2现在)
发布于 2016-11-24 16:53:58
在c++中,您所要求的是不可能的。
假设您的基础和派生可以通过header.hpp获得(临时包含在其中),并且是合法的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的另一个实例,会发生什么情况?
发布于 2016-11-24 14:36:26
抱歉,但你得决定:
std::bitset<size_t N>需要N的const(expr)号(某样东西),所以您可以通过
静态const uint32_t CounterType = 1;//计数器的实际分量数++ComponentBase::CounterType;将无法工作,因为您试图增加一个常量变量。我觉得你的设计有一些可疑之处,所以请与我们分享更多的现场信息,以便更清楚地发现问题。
https://stackoverflow.com/questions/40788739
复制相似问题