Dynamic naming of class instances

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Moezzie
    New Member
    • Jun 2007
    • 17

    #1

    Dynamic naming of class instances

    How would i go about to have class instances named after the content of a sertain variable?
    Ive posted the same question a couple of weeks back regarding Python. This time i want to do it in C++ thought.


    I tried to translate the python aproach into c++ but i dont really know how make a dictionary in, or if its even possible in c++.

    And another question, how can i then call the class instance from the contents of another variable?

    Thanks in advance
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can not dynamically name variables in C++ (or C for that matter). When you dynamically create an instance of a class it is your responsibility to maintain somewhere a pointer to the memory allocated for the instance.

    There are many ways to do this , however you seem to want to refer to the instance using either a number or a name as a reference.

    If the numbers are going to be contiguous, starting at 0 then I suggest you use a vector otherwise you may wish to look at using a map

    Comment

    Working...