#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    Manchester, England
    Posts
    39
    Rep Power
    17

    Question Arrays of template classes


    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. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    Manchester, England
    Posts
    39
    Rep Power
    17
    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!
    Practise Random Kindness

IMN logo majestic logo threadwatch logo seochat tools logo