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?
Re: Creating a variable using a variable?
Quote:
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/
Re: Creating a variable using a variable?
Quote:
Originally Posted by
2kaud
Do you know if this is possible to do in other languages?
Re: Creating a variable using a variable?
Quote:
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
Re: Creating a variable using a variable?
Quote:
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 :D
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.
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 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";
?>
Re: Creating a variable using a variable?
Quote:
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. :D
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
Re: Creating a variable using a variable?
Quote:
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. :D
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.
Re: Creating a variable using a variable?
Often times, programs are difficult enough to debug without having the variable names change on-the-fly.