3

The Problem: I've been searching on here for a while looking for a way to loop through variables named somewhat like variable_1, variable_2, ...., variable_n. Basically, I'm asking if there's a way to do that using a loop to achieve variable_i or, more specifically in my case, functionName_i.

What I need: I'm trying to loop an objects' array to call different functions which are sequentially-named and parallel to the objects' array ( i.e: obj[ i ]->callback_i( ) )

What I Know: Obviously, the answer here (if it were just variables) is using an array or vector. However, I need to just concatenate the functions' names sequentially somehow if it's possible.

Possible Workarounds: Everything I think of goes back to creating an array/vector of function pointers. I might get it eventually to work if I'm really out of options, but I just thought I should ask out of curiosity.

Clear Question: Is there a way to loop through sequentially-named functions using a variable int i as part of the functions' names?

Thanks!

ThunderStruct
  • 1,504
  • 6
  • 23
  • 32
  • 2
    No. Use an array of function pointers. – rici Aug 08 '15 at 02:18
  • 1
    To do what you want to would require using reflection, a feature not normally supported in C. – Kwarrtz Aug 08 '15 at 02:18
  • You might be able to do this depending upon your system. For example, if you're on Windows, you can put the functions in an `extern "C"` block, and export them with `__declspec(dllexport)`, then load them, by name (using a runtime string value), into function pointers with [`GetProcAddress`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683212(v=vs.85).aspx). I would imagine you can do something similar on Linux or Mac, but I do not know the process. – Benjamin Lindley Aug 08 '15 at 02:45
  • You could maybe associate the functions with a "runtime name" so something like a `std::map>`? – Galik Aug 08 '15 at 02:48
  • @Galik You mean store the function's name as they key and its pointer as the value? It's not a bad idea at all, actually... It's still storing the functions' pointer and all, but it's such a neat way of doing it – ThunderStruct Aug 08 '15 at 03:36
  • @BenjaminLindley This seems like a valid idea, actually. It's a lot of trouble, but it really answers my question since it's different (and more direct in a way) from the workaround I mentioned. Thank you for the input! – ThunderStruct Aug 08 '15 at 03:38

1 Answers1

4

No.

C++ does not usually store type or variable name information at runtime; if it does, it's not portable (typeid() does vary across compilers) or it's just not possible. You can't reference a variable name at runtime unless you make a system to do that sort of thing, or you use debugging information, which isn't a standard C++ feature.

This type of reflection is expensive and is more suited towards higher-level languages. C++, as a more lower-level language, strips off the sugar and just tells you "no."

You can make this type of thing in C++ if you make a naming system, but a generalized one would also require variants, a version of the NULL/Maybe idiom, attributes, checks, lots of debugging and you can do it all if you wish, but this is where you might as well switch to another language that already has the answer you're looking for and bind C++ to it.

Alternatively, use a matrix or array of functions. Then, iterate by index.

Community
  • 1
  • 1
CinchBlue
  • 6,046
  • 1
  • 27
  • 58