Using string containing variable path

So, lets say I have an int at "Vars.MyVars.Ints.TheInt", but I only have the 'path' saves as a string but I need to be able to send it to functions so they can access the variable.

Here's a simple example of the problem.


1
2
3
4
5
6
7
8
9
string theInt = "Vars.MyVars.Ints.TheInt";

doubleIt(theInt); // here I cant just send it as a string

int doubleIt(int* a)
{
     a += a;
     return a;
}




If anyone could help me with this that would be great :)
Last edited on
Somehow it's not clear what you want to do. You just can't convert this string
 
string theInt = "Vars.MyVars.Ints.TheInt";
to an int.
but I only have the 'path' saves as a string
What does it mean?
Ok so Ill try to show you what im really trying to do.

So I CAN do it like this and it works fine.

1
2
3
4
5
6
doubleIt(Vars.MyVars.Ints.TheInt);

void doubleIt(int* a)
{
     a += a;
}


I CANT do it like this

1
2
3
4
5
6
7
8
std::string whatever = "Vars.MyVars.Ints.TheInt";

doubleIt(whatever); // I want to use what is in the string 'whatever', as I showed above.

void doubleIt(int* a)
{
     a += a;
}


Edit: Someone on some other forums helped me explain this better.

"It seems like you're trying to input a hex string which contains an address of some variable and then you're trying to cast that string's address value into a uintptr_t to use that to edit the value at the address.

You basically have to reverse this process:

1
2
3
4
5
6
int value = 10;
auto address = reinterpret_cast<uintptr_t>(&value);
std::stringstream ss;
ss << std::hex << address;
std::string strAddr (ss.str());
std::cout << "Address of value = " << strAddr << std::endl; // just append "0x" to start if you want to format it like hex 
"
Last edited on
It seems like you're trying to input a hex string which contains an address of some variable and then you're trying to cast that string's address value into a uintptr_t to use that to edit the value at the address.
The person who said this is a moron. It's quite evidently not what you're trying to do.

What you're trying to do is called "reflection", and in languages that don't support it (such as C++) it's so cumbersome to implement that it's almost never done. Solutions are often very ugly. Qt for example has MOC as the solution, which involves a preprocessing step during compilation to extract relevant data from the sources to generate code and data structures.

Think about why you need to do this can consider:
* Do you really need this?
* Can you reduce the problem? For example, instead of accepting strings like "Vars.MyVars.Ints.TheInt", accepting only "TheInt".
If this is a very important feature, perhaps it would be better to use a different language.
So I am able to do it like this.

1
2
3
4
5
6
const std::string address_as_text = "0x12345678";
unsigned int address_as_number = 0U;
std::istringstream addr_stream(address_as_text);
addr_stream >> hex >> address_as_number;
int * pointer_to_int = (int *) address_as_number;
int value_from_memory = *pointer_to_int;


and this seems to work, now the only issue still here is that Instead of "0x12345678" I need to use the Variable path like "Vars.MyVars.Ints.TheInt"
Instead of "0x12345678" I need to use the Variable path like "Vars.MyVars.Ints.TheInt"
It can't be done in C++. Unless you compile with debugging enabled, the compiled program looses the names of the variables, so there's no "path" to bind it to.

Let's back up a little. Can you explain why you need to do this? There is probably an easier way.
Topic archived. No new replies allowed.