一連の特殊なテンプレートクラスへのインターフェイスとして機能するクラスを作成したいと思います。例えば:
template<typename T>
class ThingDoer {
public:
void Method()
{
// do something;
}
};
class ThingDoerInterface {
public:
template<typename T>
void Method()
{
// call ThingDoer<T>::Method(); somehow
}
};
int main()
{
ThingDoerInterface i;
i.Method<int>();
i.Method<char>();
// etc.
return 0;
}
私が欲しいオブジェクトの一般的な要件は次のようになります。
- ユーザーは、オブジェクトのテンプレート化されていないインスタンスを1つだけ作成する必要があります。
- ただし、複数のインスタンスが存在する可能性があり、独立していることが期待されます。
- オブジェクトは、タイプAから派生した(ユーザー定義の)オブジェクトのインスタンスを、タイプBから派生した(1つ以上の)オブジェクトに関連付けます。
- ユーザーは、Aのタイプに基づいてBで何かを行うオブジェクトのメソッドを呼び出すことができます。
私はに基づいた実際の問題に対する実用的な解決策を持っていますが、std::unordered_multimap
このようなことがテンプレートだけで実行できるかどうかに興味があります。
編集:
これは、私が実際にやろうとしていることを説明することを願っている、より具体的な例です。
class ABase {
public:
virtual ~ABase() {}
};
class A1 : public ABase {};
class A2 : public ABase {};
class BBase {
public:
virtual ~BBase() {}
};
class B1 : public BBase {};
class B2 : public BBase {};
class ThingDoerInterface {
public:
template<typename T>
void Store(BBase* b_ptr)
{
// store the B pointer with the type of T as a key
// (T will be A1 or A2)
}
template<typename T>
void Recall()
{
// call all the stored B pointers associated with the type of T
}
};
int main()
{
ThingDoerInterface i;
B1* b_one_ptr = new B1;
B2* b_two_ptr = new B2;
i.Store<A1>(b_one_ptr);
i.Store<A1>(b_two_ptr);
i.Store<A2>(b_one_ptr);
i.Recall<A1>(); // do something with b_one_ptr and b_two_ptr
i.Recall<A2>(); // do something with b_one_ptr
delete b_two_ptr;
delete b_one_ptr;
return 0;
}
そして、私はこれをで行いましたstd::unordered_multimap
が、私が知りたいのは、次のように関連付けを保存できるかどうかです。
template<typename T>
class ThingDoer {
public:
void Store(BBase* b_ptr)
{
b_ptrs.push_back(b_ptr);
}
void Recall()
{
// do something with the b_ptrs associated with the type of T
}
private:
std::vector<BBase*> b_ptrs;
};
ThingDoerInterface
しかし、どういうわけかそうします。