Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing template class as parameter

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.

like image 201
Shaun1810 Avatar asked Oct 29 '13 14:10

Shaun1810


People also ask

What is a template parameter?

In the definition of a member of a class template that appears outside of the namespace containing the class template definition, the name of a template parameter hides the name of a member of this namespace.

What are the different types of parameters in parameter-list?

Each parameter in parameter-list may be: a template template parameter. 1) A non-type template parameter with an optional name. 2) A non-type template parameter with an optional name and a default value. 3) A non-type template parameter pack with an optional name.

Is it possible to pass C-style strings as template parameters?

This resembles Ataul's approach: As it can be seen, passing C-style string s as template parameters requires quite a lot of overhead and might have other negative consequences (option 1 practically gives global scope to s ). I wonder if there is any real-life use for it.

What is the difference between a function template and a parameter pack?

In a function template, there are no restrictions on the parameters that follow a default, and a parameter pack may be followed by more type parameters only if they have defaults or can be deduced from the function arguments.


1 Answers

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

like image 150
Shoe Avatar answered Sep 28 '22 08:09

Shoe