This question already has answers here:
Convert string to variable name or variable type
(7 answers)
Closed 5 years ago.
If the title is not so suggestive I hope the explanation might help you understand my problem.
I have a function which sets some variables value. But the variables name I would like to provide it as a string, this way:
void setValue( const std:: string& variable_name, int value){ variable_name=value;}
then when I call this function like this:
setValue("variable", 10);
I will expect to execute set variable=10;
Any idea if this approach is possible or other ways I could have this behaviour?
Thanks!
It is not possible to magically retrieve a variable from a run-time string. You need to provide a mapping in advance, which is probably not what you want. E.g.
int& getFromName(const std::string& s)
{
if(s == "variable") return variable;
if(s == "foo") return foo;
// ...
}
Related
This question already has answers here:
When should I use C++14 automatic return type deduction?
(7 answers)
How to return arbitrary type in C++11 using auto keyword?
(6 answers)
Closed 8 months ago.
I don't want to declare the return value two times, so maybe there is a more clever way in modern C++ to do this, but I couldn't find any way.
So, instead of this:
int calculate() {
int ret=0;
return ret;
}
I want to do something like this:
int calculate() {
decltype(return_type) ret=0;
return ret;
}
It would make it easier to change the return type if needed.
If it's not possible, then why is it not possible?
This question already has answers here:
Simple C++ - about strings and concatenation and converting int to string [duplicate]
(3 answers)
Closed 6 years ago.
This may seem trivial to normal C++ users, but I'm currently relearning C++ all over again. I have a function that takes in an int& and I would like to add that value to a string for a print statement. So, I have something like this:
std::string getTest(void* dataGroup, int& xWidth, int& yHeight)
{
std::string results = "";
results += "\nxWidth is: " + xWidth;
return results;
}
which fails to run when I call it. What is the proper way to convert the pass-by-reference int& value into a form that can be added to a string?
std::to_string(xWidth).
The & is pretty much irrelevant. to_string takes the int by value, so how your function takes it doesn't matter: by value, lvalue ref, rvalue ref, const or non-const... all will work.
This question already has answers here:
What is a reference variable in C++?
(12 answers)
Closed 7 years ago.
I want to implement a function with a parameter that will receive a string result:
bool function (entry parameter, std::ostringstream & var)
I'm not sure about what it is, pointer ?
Should I do something special about it or just: var << result ?
The return value is boolean but we will need var after
& means it is a reference, not a pointer. See Andrew's comment link. You do not need to delete var, if that is what you mean by "something special." Given the method signature you provided, the code:
var << result;
should work just fine, assuming "result" is something sane. If you need more information, you may need to post a MCVE.
This question already has answers here:
Why does call-by-value example not modify input parameter?
(6 answers)
Closed 8 years ago.
int Test = 11;
void Toggle(const char* Name, int Offset, int Value){
Offset ^= Value;
printf("%s\n [%s^7]", Name, Offset ? "^2ON" : "^1OFF");
}
In main...
void main(){
Toggle("Unlimited Ammo", Test, 100);
Toggle("Unlimited Ammo", Test, 100);
}
I'm pretty sure XOR is suppose to toggle this. I don't know why its not working for me.
C++, by default, is pass by value, meaning the function gets a local copy of Test. Changes to it are not reflected back to the caller.
If you want pass by reference, you have to use ... wait for it ... references :-)
You do that with a function signature like:
void Toggle(const char* Name, int &Offset, int Value) ...
This question already has answers here:
c++ change function's variable argument
(2 answers)
Closed 8 years ago.
Say I have
string stringInput = "hello";
alter(stringInput);
cout << stringInput;
and a function:
void alter(string stringIn){
stringIn[0] = stringIn[3];
}
Ideally I would want cout to produce "lello". But right now it simply returns "hello" as originally. I know this has something to do with addresses and pointers... how would I achieve this?
It's actually just because a new copy of the string is created for use in the function. To modify the string directly in the function add an & before the string name in the function header like this:
void alter(string &stringIn){
That passes the string by reference. Otherwise you could just return a string from the function.
All you need to do is to pass the string by reference:
void alter(string& stringIn){
// ^
stringIn[0] = stringIn[3];
}
You should also modify accordingly any function declarations you have for alter().
Your stringIn is a local variable. So when you pass it on the function as a value it just makes a new stringIn with different address. So the changes you are making in alter is only affecting the new stringIn. You need to recieve the reference of the stringIn in alter to make it work.
void alter(string& stringIn){
stringIn[0] = stringIn[3];
}