2013-10-29 4 views
9

テンプレートクラスを別のクラスのコンストラクタに渡すにはどうすればよいですか?私は、ユーザーがハッシュテーブルの型を決定できるようにするメニュークラスにテンプレートのハッシュテーブルクラスを渡そうとしています。テンプレートクラスをパラメータとして渡す

template <class T> 
class OpenHash 
{ 
private: 
    vector <T> hashTab; 
    vector <int> emptyCheck; 
    int hashF(string); 
    int hashF(int); 
    int hashF(double); 
    int hashF(float); 
    int hashF(char); 

public: 
    OpenHash(int); 
    int getVectorCap(); 
    int addRecord (T); 
    int sizeHash(); 
    int find(T); 
    int printHash(); 
    int deleteEntry(T); 
}; 

template <class T> 
OpenHash<T>::OpenHash(int vecSize) 
{ 
    hashTab.clear(); 
    hashTab.resize(vecSize); 
    emptyCheck.resize(vecSize); 
    for (int i=0; i < emptyCheck.capacity(); i++) 
    { 
     emptyCheck.at(i) = 0; 
    } 
} 

だから、などを、私は、テンプレート化され、このクラスのオープンハッシュを持って、それが追加される任意のタイプのためにできるようになっているので、私のメインでのオブジェクトを開始した場合、私はこの作業を持っており、入力タイプを変更

int main() 
{ 
    cout << "Please input the size of your HashTable" << endl; 
    int vecSize = 0; 
    cin >> vecSize; 
    cout << "Please select the type of you hash table integer, string, float, " 
      "double or char." << endl; 
    bool typeChosen = false; 
    string typeChoice; 
    cin >> typeChoice; 
while (typeChosen == false) 
{ 
    if (typeChoice == "int" || "integer" || "i") 
    { 
     OpenHash<int> newTable(vecSize); 
     typeChosen = true; 
    } 
    else if (typeChoice == "string" || "s") 
    { 
     OpenHash<string> newTable(vecSize); 
     hashMenu<OpenHash> menu(newTable); 
     typeChosen = true; 

    } 
    else if (typeChoice == "float" || "f") 
    { 
     OpenHash<float> newTable(vecSize); 
     typeChosen = true; 
    } 
    else if (typeChoice == "double" || "d") 
    { 
     OpenHash<double> newTable(vecSize); 
     typeChosen = true; 
    } 
    else if (typeChoice == "char" || "c" || "character") 
    { 
     OpenHash<char> newTable(vecSize); 
     typeChosen = true; 
    } 
    else 
    { 
     cout << "Incorrect type"; 
    } 
} 
return 0; 
} 

私のメインでは、どのようなタイプのハッシュテーブルを作成するかをユーザーに尋ねたいと思います。彼らが何を入力するかに応じて、このクラスのインスタンスを作成し、これをメニューと呼ばれる別のクラスに渡す必要があります。このクラスはハッシュクラスから関数を呼び出すことができます。

+0

あなたはクラステンプレートまたはクラステンプレートの特殊化を通過したいですか?例えば。 'std :: less'または' std :: less '? – dyp

+0

hashFは私のハッシュ関数です。テンプレート化されたクラスを渡したいと思います。 – Shaun1810

答えて

13

あなたが使用することができます。

class Ctor { 
public: 
    Ctor(const Other<int>&); // if you know the specific type 
}; 

かを:

class Ctor { 
public: 
    template<class T> 
    Ctor(const Other<T>&);  // if you don't know the specific type 
}; 

Live demo

関連する問題