類型考慮以下方法C++特異於TYPE_INFO
static ComponentType & getTypeFor(const type_info &t){
ComponentType * type = componentTypes[t.hash_code()];
if(type == NULL)
{
type = new ComponentType();
componentTypes[t.hash_code()] = type;
}
return *type;
};
static bitset<BITSIZE> getBit(const type_info &t){
ComponentType & type = getTypeFor(t);
return type.getBit();
}
我稱這個如下
ComponentManagerType::getBit(typeid(MyComponentClass));
// Not an instance, passing class name
現在作爲ComponentManagerType建議;這僅適用於組件。目前的問題是任何類型都可以通過。它不會造成任何傷害,但是會爲非組件對象創建id和bitset。
問: 如何強制此方法僅接受基本類型Component的對象?
我知道沒有直接的辦法。但是我非常在意這點。
編輯: 添加我自己的解決方案。不知道它是否猶太教。
是不是可以用boost:is_base_Of? – GL770
看起來您想使用模板。除非你有非常明確的理由,否則選擇RTTI而不是C++中的其他解決方案是不理想的。 – kennytm
GL770截至目前,我對boost沒有興趣。@ kennyTM我想了很多。 – Sidar