CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    3

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2012
    Posts
    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.

  4. #4
    Join Date
    Oct 2008
    Posts
    1,456

    Re: template class instance

    Quote Originally Posted by gotfell View Post
    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.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: template class instance

    Quote Originally Posted by gotfell View Post
    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

  6. #6
    Join Date
    Oct 2012
    Posts
    3

    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
  •  





Click Here to Expand Forum to Full Width

Featured