-
October 20th, 2012, 05:34 PM
#1
template class instance
Hello, i have a class that is a template, I have to declare it in my main but i want the user to choose what type of data they will use in the class, I cant just declare myclass my, i have to use myclass<int> my, how can I change so user can select the proper datatype to use in the class.
kindest regards
thanks
-
October 20th, 2012, 11:51 PM
#2
Re: template class instance
The choice of the template argument is determined at compile time, not run time, so taken at face value, what you are asking for cannot be done.
Anyway, what types may the user choose?
-
October 21st, 2012, 02:58 AM
#3
Re: template class instance
its for a hashtables that could contain char,string,int,double,float. maybe its better i create many hashtables of types i need and just overload functions to put the data into correct hashtable.
-
October 21st, 2012, 11:16 AM
#4
Re: template class instance
Originally Posted by gotfell
its for a hashtables that could contain char,string,int,double,float.
you can instantiate the template with a type modeling all of them, dynamically. You can use boost variant for a ready-made solution ( if I recall correctly, it automatically inherits the comparison operators of its costituent types, so it would work immediately with associative containers; that said, adding hash support should be easy anyway ); otherwise, you can easily write your own "dynamic" type implementation ...
of course, as mentioned by laserlight, note that all this would defeat the benefit of compile time instantiation.
-
October 21st, 2012, 04:10 PM
#5
Re: template class instance
Originally Posted by gotfell
its for a hashtables that could contain char,string,int,double,float. maybe its better i create many hashtables of types i need and just overload functions to put the data into correct hashtable.
Why do you want to instances of those types in a single container? If you describe at a higher level what you want to achieve, you'll probably get better advice on what design is suitable.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 21st, 2012, 04:21 PM
#6
Re: template class instance
Hi, Sorry i was a big vague, no i didnt want them in a single container, i just wanted to save having many hashtables lying around, but actually its worked out better making tables of ints, char and the rest and having overloads just place them in the right container. Thanks to the original reply i know a bit more about using templates in my hashmaps.
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|