-2

Is it possible to create a new variable for a function everytime the function is called? I was considering making a huge array and just using a different block for each time a function is called but I would prefer to create variables on demand instead, is this possible in c++?

Example: A user types asdf and clicks save, setting off the savetext function

void savetext(textvariable)
{
static int //(this variable name should somehow become asdf) = somedata;
return;
}
user1934608
  • 405
  • 1
  • 4
  • 9
  • 2
    Sounds like dynamic allocation (using `new`) is the answer, but could you give an example of what exactly you are trying to do? – jogojapan Dec 29 '12 at 12:53
  • huge array = use a `std::vector<>`; outside of that, not really sure where you're going with the whole "create variables" approach. – WhozCraig Dec 29 '12 at 12:53
  • 6
    Variable values are not "remembered" for the next call of the function anyway, unless they are `static`. – DCoder Dec 29 '12 at 12:53
  • Do you mean new block for the function? Seems to me you would like to construct an array of pointers to functions. Please give a more detailed example of what you want to achieve. – Ivaylo Strandjev Dec 29 '12 at 12:56
  • I'm still mostly in the process of learning programming although a specific desired use prompted me to ask this question I don't want to include it because there are multiple other solutions and I wanted to know the answer to this for future references as well, not just solve the problem at hand. – user1934608 Dec 29 '12 at 12:57
  • Honestly your question doesn't make sense... I think the closest thing to what you're asking for is delayed initialization. – user541686 Dec 29 '12 at 13:00
  • Variables are exclusively a compile-time concept. As such you cannot _create_ variables at run-time. I'm having a feeling that the question you asked is not what you meant to ask. Consider rephrasing your question using appropriate terminology. – IInspectable Dec 29 '12 at 13:08
  • 1
    Now, after your edit, it sounds like it's a duplicate of one of these questions: http://stackoverflow.com/questions/7143120/convert-string-to-variable-name-or-variable-type, http://stackoverflow.com/questions/13220957/generating-variables-names – jogojapan Dec 29 '12 at 13:11
  • Maybe what you need is `std::map`? – Greg Dec 29 '12 at 13:15

2 Answers2

1

If you have something that, for example, adds things to an array, then vector is the right solution. The vector class will automatically grow as you need it, using the push_back function to add things to itself.

The vector class acts largely as an array that grows as you need it to, so it's very easy to use.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • No, the default for "I need to store a bunch of stuff" is vector. Only when you have shown somehow that vector isn't good enough (either can't do what you want, or some performance reason) should you use other things. – Mats Petersson Dec 29 '12 at 13:04
  • `std::vector<>` is the right solution if, and **only** if, you are required to have the controlled sequence be in contiguous memory. More often than not when people recommend `std::vector<>` they actually meant `std::deque<>`. – IInspectable Dec 29 '12 at 13:04
  • 1
    This may be what I'm looking for, I'll look into this, thankyou. – user1934608 Dec 29 '12 at 13:04
  • I'd suggest that you use a std::vector if you want to save a bunch of text. Let each entry be a line of text, or some such. – Mats Petersson Dec 29 '12 at 13:12
1

Every time you enter a function, all variables in it are new. There's no need to explicitly create variables unless you need objects that live longer than your function, or you don't know how many you need.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • That's part of what I was implying, that the variables would basically now act similar to global variables in that they would outlive my function, but I'm not sure static would solve the problem unless I can make a dynamic name for the variable each time it's called. – user1934608 Dec 29 '12 at 13:03