2013-10-29 38 views
9

Şablonlu bir sınıfı başka bir sınıfın kurucusuna nasıl iletirim? Ben bir kullanıcı sınıfı tablo sınıfına karar vermemize izin verecek bir menü sınıfına templated bir hash tablo sınıfı geçmeye çalışıyorum. o vbŞablon sınıfını parametre olarak geçirme

girdi türlerini benim ana bunun bir nesneyi başlatmak ve değiştirirseniz, bu çalışma herhangi bir tür eklenecek izin gerekiyordu çünkü

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; 
    } 
} 

Yani, şablon olarak değiştirilebilir bu sınıf Açık karmaya sahip

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; 
} 

Ana sayfamda kullanıcılara hash tablosunu hangi tipte yapacaklarını sormak istiyorum. ne girdiklerine bağlı olarak, istedikleri türle bu sınıfın bir örneğini oluşturmalı ve bunu daha sonra karma sınıfı işlevlerini çağırmasına izin vermesi gereken menü denilen başka bir sınıfa geçirmelidir.

+0

bir sınıf şablonu veya bir sınıf şablonu ihtisas geçmek istiyor musunuz? Örneğin. std :: less' veya std :: daha az '? – dyp

+0

hashF benim karma işlevimdir. ve şablonlanmış bir sınıfı geçmek istiyorum. – Shaun1810

cevap

13

Sen kullanabilirsiniz:

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

ya:

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

Live demo

İlgili konular