-1

Is there a way to save a variable in an std::string and use it later? In this code I expect the output to be 41

#include <iostream>

int main(){
  std::string number [5] = {1,2,3,4,5};

  std::string join = "number[3]+number[0]";

  std::cout << join; // Expect result should 41.

  return 0;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
IOSBET
  • 61
  • 1
  • 1
  • 6
  • @NutCracker almost. This way it adds up the numbers instead of concatenating them, but `std::string(number[3]) + std::string(number[0])` should do the trick. – Blaze Jul 03 '19 at 08:46
  • @Blaze the question is how i can psas the number[3] inside std::string in string? i means the number[3] inside my database. – IOSBET Jul 03 '19 at 08:50
  • 1
    You can't turn a string containing `"number[3]"` back into a reference to `number[3]`. That's a feature called "reflection" and C++ doesn't have that (at least not yet). There are several workarounds depending on what your requirements are. – Blaze Jul 03 '19 at 08:52
  • @Blaze yeah, reflection. i forgot to mention in question. i need to port my php code that use eval into c++. I still can't move. stuck in here. I need to reference back array index from string and concatenate it. – IOSBET Jul 03 '19 at 08:57
  • 1
    I would recommend posting a new question where you explain your porting task in greater detail. Also include the PHP code that you are porting and explain what you can't do (and why). Make sure to also tag [tag:php] so people who are more knowledgeable in this language can also have a look. The better you explain your task, the better people can recommend an adequate workaround to deal with the missing reflection in C++. – Blaze Jul 03 '19 at 09:06
  • @Blaze i knows chunk of function that i need to figure it out. its a eval in php. how to implement this in c++. i splits the problem in chunk. Eval can execute a code from string. – IOSBET Jul 03 '19 at 09:11
  • [Is there C/C++ equivalent of eval(“function(arg1, arg2)”)?](https://stackoverflow.com/a/11078610/7582247) – Ted Lyngmo Jul 03 '19 at 18:18

1 Answers1

3

It rather should look like this:

std::string number[5] = { "1", "2" , "3" , "4", "5" };
std::string join = number[3] + number[0];

Your problem was that you declare an array of strings, but tried to initialize it with integer values. Another issue was in "number[3]+number[0]" statement which must not be a string but call of operator+ of standard string.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • 3
    @amlucas isn't that what OP wanted? *"Expect result should 41."* – Blaze Jul 03 '19 at 08:47
  • I knows that. but, i need to use variabel as string. cause the index. i need to access is random. sometimes. the variabel of index i save in database. – IOSBET Jul 03 '19 at 08:47
  • 1
    @amandayui if you want something else than what you wrote in the question you should have written what you want in the quesiton ;). Maybe open a new one... – 463035818_is_not_an_ai Jul 03 '19 at 09:01
  • no, i want is reflection. i need to pass reference as string. and use it later. In question, i think is clear. – IOSBET Jul 03 '19 at 09:06