継承を使用せずにこれを行う方法は2つありますが、これはいくつかの決定要因にも左右されます。最初の質問は:外部クラスのインスタンスを作成する際には、既知のテンプレートコンテナのタイプです。そうであれば、このクラスは簡単に書くことができます。そうでなければ、それでもやることができますが、もっと多くの作業が必要になります。大文字小文字が分からない場合は、これを行う2つの方法があります。最初は、これをクラステンプレートにしないことで回避しようとしているものです。 2番目の方法は、このクラスを定義する方法のいくつかの論理を示すより多くの作業を含んでいます。
擬似コード: - 第一ケースタイプは、タイプがラッパーをテンプレート化することなく、メソッドを使用して知られていない第二の場合について
#include <map>
template<class T>
class MyContainer {
// ... Class Variables, Constructors & Methods
}
// For Demonstration We will say that `T` is known to be an int upon instantiation
class MyClass {
private:
std::map< std::string, MyContainer<int> > maps_;
public:
MyClass() {}
~MyClass() {
// Clear Out Map
}
void addItem(std::string& str, int value) {
maps_.insert(std::make_pair(str, MyContainer<int>(value));
}
};
今作成時に知られている、あなたはを知っておく必要がありますこのクラスがサポートできるすべてのタイプがあり、これらのそれぞれのtypedef
を作成する必要があります。
擬似コード - タイプが知られていない第二ケース:
#include <map>
template<class T>
class MyContainer {
// ... Class Variables, Constructors & Methods
}
// For Demonstration We will say that `T` is unknown before instantiation
class MyClass {
public:
typedef MyContainer<int> INTS;
typedef MyContainer<float> FLOATS;
typedef MyContainer<double> DOUBLES;
// And Do This For Every Type This Class Will Support.
private:
std::map< std::string, INTS > mapInts_;
std::map< std::string, FLOATS > mapFloats_;
std::map< std::string, DOUBLES > mapDoubles_;
// And You Will Need A Container For Each Supporting Type
public:
MyClass() {}
// If You Have Constructors Other Than Default That Excepts Parameter Types
// You Will Need A Constructor For Each Supporting Type
~MyClass() {
// Clear Out All Maps
}
void addInts(std::string& str, MyClass::INTS);
void addFloats(std::string& str, MyClass::FLOATS);
void addDoubles(std::string& str, MyClass::DOUBLES);
// And You Will Need A Corresponding Function For Each Type This Class Supports.
};
「テンプレートクラス」のようなものはありません。 C++にはクラステンプレートがあります。クラステンプレートはクラスではありません。あなたの説明ははっきりしていません。コードをもっと表示してください。 –