linked list of templates (or a design flaw?)

This is a discussion on linked list of templates (or a design flaw?) within the C++ Programming forums, part of the General Programming Boards category; Hey, This may seem a bit of an odd question, but I'm trying to store a list of templates of ...

  1. #1
    Ess
    Ess is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    linked list of templates (or a design flaw?)

    Hey,

    This may seem a bit of an odd question, but I'm trying to store a list of templates of different types, I don't know if this is possible or not, but all the efforts I've made so far have failed miserably.
    I tried a DIY list but I kept getting compilation errors about being unable to specialize template functions, and the STL list errors because the use of a class template requires an argument list.

    The reason I'm doing such a thing is to store variables the user can alter at runtime, for instance there may be an integer, character string and floating point variable and the user can go:

    myheight 5.5
    myname "fred"
    etc...

    I was storing them in a list by registering them:

    RegisterVariable(&userheight, "myheight");

    For each variable I was storing the name so it would be possible for me to look it up when the user tries to alter it, going something like

    proper_variable = FindVariable(argument[0]); // would be a string like "myheight", indicating the name to edit

    then just after

    proper_variable->SetValue(argument[1]);

    Is there any kind of mechanism to support what I'm trying to do, or if there's fundamental flaws in the design I'd appreciate ideas on a better way.

    Thanks,

    sdtm

    PS:

    Some more background, if you're interested.
    I tried a derived class approach but this is a pain because you have to derive a new class for each type you want, and because the types you are setting / returning may be different you can't have the get/set functions in the base class (unless you return/pass by void * - but thats getting pretty messy).

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    An easy way is this:
    Code:
    class DataType
    {
      float fvalue;
      string strvalue;
      //..
    };
    
    map<string,DataType> vars;
    You can also create derived classes, but store the pointers instead and use dynamic_cast<>.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Probably a design flaw.

    If you really want to do it, however, you can make a base class and derive the templated class from that base class, so that way they can all be of the same type. Then you can enumerate values for the child types and cast them to that type when you use them, but it sure ain't moral.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Check out the libraries available at boost
    Look at the Any class that might help you.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21