Pointer to template class, of any type

Dec 18, 2010 at 12:21pm
I've run into a brick wall here, needing to have an array/or single variable (not necessary for each element to be equal in size) each of the same data type, allowing for any template value used in that class to be used.

For example, the idea of:
template<class T>
class Test
{

};

Test* CurTest = new Test<random class>;

Really cant think of a solution other than boost varient data type, although I do not want the performance hit really. Maybe a void*, although this would be a last resort.

Any kind of suggestions are welcome,
Thanks.
Last edited on Dec 18, 2010 at 12:22pm
Dec 18, 2010 at 12:41pm
If all the array elements would have the same T you can write another class template to wrap the pointer
Dec 18, 2010 at 12:58pm
They would all have template T, it just will not necessarily be of the same type.
Would wrapping involve a pointer for each possible combination type?
Dec 18, 2010 at 1:01pm
So you may have something like this
1
2
CurTest[0] = Test<int>;
CurTest[1] = Test<double>;
Right?

That would be a bit ptoblmatic
Dec 18, 2010 at 1:09pm
How about something like this:

1
2
3
4
5
6
7
8
9
10
11
class AbstractTest { /*...*/ };

template <class T>
class Test : public AbstractTest { /*...*/ };

//...

AbstractTest * cur_test = new Test<ClassA>;
AbstractTest * next_test = new Test<ClassB>;

//etc... 

Or that type-erasure thing -> http://www.cplusplus.com/forum/articles/18756/
Dec 18, 2010 at 1:22pm
Interesting, this could be it. If I were to access the functions from AbstractTest, I would have to make all virtual right?
Dec 18, 2010 at 1:33pm
Well, yes, I suppose. Here is something similar that might help -> http://cplusplus.com/forum/general/33247/
Topic archived. No new replies allowed.