Template class help

closed account (17pz3TCk)
Hi all, I'm working on a project that creates a stack using a template class. My problem is I'm trying to make it so the user decides what kind of data type(int, string, float, etc.) the stack will use but I can't seem to think of a way to instantiate the template class with declaring a type for it before compile-time. IS what I'm trying to accomplish even feasible or do I have to set the data type before it compiles?
Templates are instantiated at compile-time. The most you can do is to allow the user to choose between a few predefined types.
closed account (17pz3TCk)
So what you are saying is I can't let the user choose the type at run-time unless its already made in the program?
Yes. Besides, how were you planning on creating objects of the selected type anyway? And what were their values supposed to be? Try to explain what you're trying to do, because there's always a solution (unless it was a question of theoretical nature).
closed account (17pz3TCk)
Thanks for your answers, they're exactly what I needed.

As for how I tried, I got input using a simple command line menu and I tried using a typedef statement depending on the input. Once the program got intput, the code followed was
1
2
3
4
5
6
7
8
9
10
if(ans==1)
		typedef int dataType;
	else if(ans==2)
		typedef double dataType;
	else if(ans==3)
		typedef float dataType;
	else if(ans==4)
		typedef string dataType;

myStack<dataType> s


So no matter what the input, the template would be of dataType. Unfortunately it didn't work like I had hoped.
closed account (D80DSL3A)
Can I cause some trouble with a vector<void*> here?

Not saying the following is a good idea, just that it's possible:
1) Prompt user for the type to be stored. I'll use a char code for this:
char typeCode;// char = 'c', int = 'i', float = 'f'
and store pointers to the values in a vector:
vector<void*> pDataVec;

2) Prompt user for data values in a switch statement:
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch( typeCode )
            {
            case 'c':{
                char ch;
                cout << "enter a character: "; cin >> ch;
                pDataVec.push_back( (void*)new char(ch) );
                break; }
            case 'i':{
                int ival;
                cout << "enter an integer: "; cin >> ival;
                pDataVec.push_back( (void*)new int(ival) );
                break; }
            case 'f':{
                float fval;
                cout << "enter a float: "; cin >> fval;
                pDataVec.push_back( (void*)new float(fval) );
                break; }
            }

Output the values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// display the values
        cout << "You entered:" << endl;
        for( unsigned i = 0; i < pDataVec.size(); ++i )
        {
            switch( typeCode )
            {
            case 'c':
                cout << *( (char*)pDataVec[i] ) << endl;
                continue;
            case 'i':
                cout << *( (int*)pDataVec[i] ) << endl;
                continue;
            case 'f':
                cout << *( (float*)pDataVec[i] ) << endl;
                continue;
            }
        }

Finally, cleanup:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// cleanup
        for( unsigned i = 0; i < pDataVec.size(); ++i )
        {
            switch( typeCode )
            {
            case 'c':
                delete (char*)pDataVec[i];
                continue;
            case 'i':
                delete (int*)pDataVec[i];
                continue;
            case 'f':
                delete (float*)pDataVec[i];
                continue;
            }
        }
        pDataVec.clear();


But this would be a ridiculous thing to actually do in practice!
1
2
3
4
5
6
7
8
9
10
11
template <class T>
void foo();

	if(ans==1)
		foo<int>();
	else if(ans==2)
		foo<double>();
	else if(ans==3)
		foo<float>();
	else if(ans==4)
		foo<string>();
closed account (17pz3TCk)
I like the idea fun2code, but we are not allowed to use anything from the Standard Template Library.

ne555 I tried something like that and it said I had an error with scope.
Topic archived. No new replies allowed.