-
June 16th, 2013, 12:05 PM
#1
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?
-
June 16th, 2013, 12:22 PM
#2
Re: Creating a variable using a variable?
Originally Posted by akiel123
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 16th, 2013, 12:25 PM
#3
Re: Creating a variable using a variable?
Originally Posted by 2kaud
Do you know if this is possible to do in other languages?
-
June 16th, 2013, 12:27 PM
#4
Re: Creating a variable using a variable?
Originally Posted by akiel123
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
-
June 16th, 2013, 01:54 PM
#5
Re: Creating a variable using a variable?
Originally Posted by Paul McKenzie
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
-
June 16th, 2013, 02:13 PM
#6
Re: Creating a variable using a variable?
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.
-
June 16th, 2013, 03:28 PM
#7
Re: Creating a variable using a variable?
Originally Posted by akiel123
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"; ?>
Last edited by Igor Vartanov; June 16th, 2013 at 03:33 PM.
Best regards,
Igor
-
June 16th, 2013, 03:48 PM
#8
Re: Creating a variable using a variable?
Originally Posted by akiel123
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
-
June 17th, 2013, 07:12 AM
#9
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
-
June 17th, 2013, 07:17 AM
#10
Re: Creating a variable using a variable?
Originally Posted by Igor Vartanov
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.
-
June 18th, 2013, 12:11 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|