C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 28th, 2003, 05:50 PM
majelbstoat's Avatar
majelbstoat majelbstoat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Manchester, England
Posts: 39 majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 h 4 m 46 sec
Reputation Power: 16
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

Reply With Quote
  #2  
Old April 29th, 2003, 10:24 AM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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.

Reply With Quote
  #3  
Old April 29th, 2003, 05:38 PM
majelbstoat's Avatar
majelbstoat majelbstoat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Manchester, England
Posts: 39 majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 h 4 m 46 sec
Reputation Power: 16
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Arrays of template classes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap