Passing template class as parameter

21,554

You can use:

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

or:

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

Live demo

Share:
21,554
Shaun1810
Author by

Shaun1810

Updated on July 09, 2022

Comments

  • Shaun1810
    Shaun1810 almost 2 years

    How do I pass a templated class to the constructor of another class? I am trying to pass a templated hash table class to a menu class which will allow me to then allow the user to decide the type of the hash table.

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

    So I have this class Open hash that is templated, because it supposed to allow for any type to be added, I have this working if initiate a object of it in my main and change input types etc.

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

    In my main I want to ask the user what type they which to make the hash table. depending what they enter it should create a instance of this class with the type they want and then pass this to another class called menu which should allow them to call functions from the hash class.