get_component_type_id<BaseClass>
とget_component_type_id<ChildClass>
は2つの異なる機能です。したがって、static size_t uniqueComponentId
の2つが得られ、それぞれ独自の値があります。
アップデートは、OP
でコメントする応答はい、それは可能です。
template <typename T>
inline std::size_t get_component_type_id(T*, std::false_type) noexcept
{
static size_t uniqueComponentId{__INTERNAL__::getUniqueComponentId()};
return uniqueComponentId;
}
inline std::size_t get_component_type_id(BaseClass*, std::true_type) noexcept
{
static size_t uniqueComponentId{__INTERNAL__::getUniqueComponentId()};
return uniqueComponentId;
}
template<typename T>
inline std::size_t get_component_type_id() noexcept
{
static_assert(std::is_base_of<Component, T>::value, "T must be of type Component.");
return get_component_type_id((T*)nullptr, typename std::is_convertible<T, BaseClass>::type{});
}
ただし、それは壊れやすいです。 Component
から派生した別のクラスに対して同じ振る舞いをしたい場合は、大きな変更を加える必要があります。
virtual
メンバー機能を使用する方がよいでしょう。あなたが合うよう
struct Component
{
virtual size_t get_type_id() const = 0;
};
struct BaseClass : Component
{
size_t get_type_id() const
{
static size_t uniqueComponentId{__INTERNAL__::getUniqueComponentId()};
return uniqueComponentId;
}
};
struct ChildClass : BaseClass {};
今、あなたは、継承階層の任意のレベルでsize_t get_type_id() const
を実装することができます。
の可能性のある重複した[.hファイルに定義を持つテンプレートクラスの静的メンバ変数にどうしますか](http://stackoverflow.com/questions/7108914/what-should-happen-to-template- class-static-member-variables-with-definition-in) – kfsone