Is there any way to change the template of an object?
Such that
vector <int> list(10);
can be converted to
vector <char> list(10);
and stored in the same vector?
Or is there a way to fake it?
possibly using typename?
Is there any way to change the template of an object?
Such that
vector <int> list(10);
can be converted to
vector <char> list(10);
and stored in the same vector?
Or is there a way to fake it?
possibly using typename?
Last edited by Trauts; 04-09-2003 at 12:21 PM.
Couple of possibilities come to mind, depending on if you want to store multiple data types in one object or create a separate object for each data type.
If you want an object for each data type, create a template using "template <class T>"
If you are trying to store multiple data types in one object, you are most likely going to have to convert to strings to go into the object, then convert back to whichever data type you started with to use it. That seems pretty difficult to me.
I want to make the datatype the template represents different.
Basically, is there a way to make something like
Where you declare one like this:Code:template <class a> class datatype { public: a data; };
datatype<char> name;
What I want to do is make THE SAME variable correspond to an object with type
datatype<int>
so that I can store an unknown datatype, but then later make the same variable store a different type.
In other words, I'd like to be able to do something like this:
Code:#include <iostream.h> template <class a> class datatype { public: a data; }; int main() { datatype *name; name = new datatype<int>; name.data = 50; cout << name.data << endl; //should be 50 delete name; name = new datatype<char>; name.data = 'a'; cout << name.data << endl; //should be 'a' delete name; return 0; }
Last edited by Trauts; 04-09-2003 at 09:02 PM.