Creating a variable using a variable?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Creating a variable using a variable?

  1. #1
    Join Date
    Jun 2013
    Posts
    6

    Smile Creating a variable using a variable?

    Hi guys.
    Okay, so I am quite new to programming, and i was wondering if there is any way to create a variable using a variable in the name?
    So f.x. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1? How would you do this?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,999

    Re: Creating a variable using a variable?

    Quote Originally Posted by akiel123 View Post
    Hi guys.
    Okay, so I am quite new to programming, and i was wondering if there is any way to create a variable using a variable in the name?
    So f.x. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1? How would you do this?
    You wouldn't. That's not how c++ works with variable naming.

    See
    http://www.learncpp.com/
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jun 2013
    Posts
    6

    Re: Creating a variable using a variable?

    Quote Originally Posted by 2kaud View Post
    You wouldn't. That's not how c++ works with variable naming.

    See
    http://www.learncpp.com/
    Do you know if this is possible to do in other languages?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Creating a variable using a variable?

    Quote Originally Posted by akiel123 View Post
    Do you know if this is possible to do in other languages?
    On a high-level, what exactly are you trying to achieve with this strange request of naming variables using integers?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2013
    Posts
    6

    Re: Creating a variable using a variable?

    Quote Originally Posted by Paul McKenzie View Post
    On a high-level, what exactly are you trying to achieve with this strange request of naming variables using integers?
    Isn't C++ a high-level programming language?. I haven't got any practical examples mostly just want to know

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    Re: Creating a variable using a variable?

    Quote Originally Posted by akiel123
    Do you know if this is possible to do in other languages?
    Yes, e.g., PHP allows for variable variables. That said, beginners to PHP have a tendency to abuse the feature by using them in situations where an associative array would be more appropriate. In standard C++, you can use std::map or std::unordered_map when you need an associative array, hence there usually is no disadvantage in lacking such a feature.
    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

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: Creating a variable using a variable?

    Quote Originally Posted by akiel123 View Post
    Do you know if this is possible to do in other languages?
    Yes it is possible. PHP is able to do that, and this is what I despise it for.

    PHP Code:
    <?
    function foo($val)
    {
        return 
    "Value by $val";
    }

    // assign index
    $x 1;

    // make variable name by index
    $name "nr$x";

    // assign new variable value indirectly
    $$name foo($x);

    // access the new variable explicitly
    echo ">> $nr1\n";
    ?>
    Code:
    >> Value by 1
    Last edited by Igor Vartanov; June 16th, 2013 at 03:33 PM.
    Best regards,
    Igor

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,877

    Re: Creating a variable using a variable?

    Quote Originally Posted by akiel123 View Post
    Isn't C++ a high-level programming language?
    You won't believe it, but it seems not. You can scratch it a little, and under a thin laquer coating you find that pure old C stuff which some people consider a slightly modified version of assembler. So basically C++ is as much high-level as assembler is.
    Best regards,
    Igor

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,643

    Re: Creating a variable using a variable?

    somewhat unclear, but what you want is either static naming at compiletime which you can do with macro's. This is called "token pasting". See here: http://www.parashift.com/c++-faq/mac...n-pasting.html


    the dynamic creation of named variables in languages like php, javascript, lua is really not a "variable" at all, rather it's either a implemented as a dictionary or an associative array (which can be either single level or multilevel). However, this isn't very usefull in C++, this type of data structure is typically used in scripted languages (or that have a script background).
    http://en.wikipedia.org/wiki/Associative_array

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,643

    Re: Creating a variable using a variable?

    Quote Originally Posted by Igor Vartanov View Post
    You won't believe it, but it seems not. You can scratch it a little, and under a thin laquer coating you find that pure old C stuff which some people consider a slightly modified version of assembler. So basically C++ is as much high-level as assembler is.
    tsk... C++ is a high level language...

    and having "variable variables" has nothing to do with a language being high level or not. variable variables is just a lot of compiler/language gunk/gloop/candy on top to hide the fact that your "variable" is really a lot more than a simple variable.
    there are plenty languages that are much "higher" level than php that don't offer variabel variables, and the other way around. Don't mistake a language/library feature for how "high" a language is.


    There are a number of ways you could simulate the behaviour/effect in C++ although without full compiler assistance for it, it'll be a bit rougher around the edges.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: Creating a variable using a variable?

    Often times, programs are difficult enough to debug without having the variable names change on-the-fly.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center