|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
Is it possible to have an array of a template class that can take different instantiations for each element? What I mean is, I declare a class like this:
Code:
template <class T> myclass
{
T var1;
T var2;
myclass(T v1, T v2) {var1 = v1; var2 = v2;}
int method1(T param);
// .. Rest of class definition ..
}
Can I then have an array of myclasses, but whose elements can be myclass<double>, myclass<int>, myclass<string> etc? At the moment, I'm trying to do it using an array of plain myclass pointers: Code:
myclass *my_array[MAX_LENGTH]; int numVars = 0; and then creating each element like this: Code:
// To create a myclass<int> myarray[numVars++] = reinterpret_cast<myclass *> (new myclass<int> (1, 2); // To create myclass<double> myarray[numVars++] = reinterpret_cast<myclass *> (new myclass<double> (2.0, 3.0); and so, for each required element type. (These are just extracts from methods BTW). This seems to work fine, with the cast getting rid of the compiler errors (though I'm not sure if it will mess things up memory-wise - bit confused about that!). The problem I have is that I then can't run method1 properly because at run time it can't be determined what the type of object it actually is. Code:
myarray[0].method1(); and similar lines cause an "INTERNAL COMPILER ERROR" in VC++ which seems pretty fatal to me! Obviously, I'll also need to know the correct type to pass for method1's parameter. I'd like to cast back to the original type and I tried having a type_info attribute in the class, but discovered that typeid()'s return value is private and can't be stored for future reference. :-( As far as I know, there's a class in Java similar to vector, except that you can dump objects of any type into it. Does C++ have anything like this, and if not why not?! :-) Does anybody have any suggestions? The more I read this, the more confusing it sounds, so I'll leave it there with apologies if it makes no sense at all! All help greatly welcomed! Thanks :-)
__________________
Practise Random Kindness |
|
#2
|
||||
|
||||
|
hi, not sure if u can do the above, but i could offer another option that i think would work. maybe you already knew this, but here it goes anyways:
-make a base class, and then derive children for each different data type(vector, int, double). then u can just make an array of base class pointers, and call each method polymorphically. |
|
#3
|
||||
|
||||
|
Thanks, I'm going to have a go doing that. I had thought of doing it that way, but wanted to do it the elite way :-) Never mind!
Cheers for the reply! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Arrays of template classes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|