How to do it?

Discussion in 'C++' started by Stiopa, Sep 3, 2010.

  1. Stiopa

    Stiopa New Member

    Joined:
    Sep 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Example 1:
    Code:
    int variable0 = 0; 
    int variable1 = 1;
    int variable2 = 2;
    int varabile3 = 3;
    
    int someArray[4] = {10, 20, 30, 40};
    
    //now i can access array indices like: 
    someArray[variable0];
    someArray[variable1];
    someArray[variable2];
    someArray[variable3]; 
    
    Now I would like to do something like example above on the runtime:

    Example 2:
    Code:
    void addVariableName(std::string name) 
    {
    //add variable name and assign following number to it starting from 0
    }
    
    addVariableName("variable0");
    addVariableName("variable1");
    addVariableName("variable2");
    addVariableName("variable3");
    
    someArray[/*????*/]; //want to access element 0 here using string "variable0"
    someArray[/*????*/]; //want to access element 1 here using string "variable1"
    someArray[/*????*/]; //want to access element 2 here using string "variable2"
    someArray[/*????*/]; //want to access element 3 here using string "variable3" 
    Is there any way to do it? (in the same time as in example 1)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice