Ollie
Ollie

Reputation: 196

C++ Pointers help?

I need a little bit of help with using pointers in C++. Sorry to seem beginner but I really can't quite understand them. I have read the tutorial on pointers on the cplusplus.com website, so please don't suggest that.

I basically have a variable which holds the name of another variable, and I wish to access that variable through the holder one. I believe I need to use pointers, correct me if I'm wrong though.

E.g.

int a;
string b;

a = 10;
b = "a";

I need to access the variable "a" through the contents of variable "b".

Just to put this into better perspective, this is how I am using it:

int a;
a = 20;

void getVar(string name) {
    cout << name;
}

getVar("a");

But as you can see, on the fifth line, that will just cout the value of name, in this case "a", but I want it to cout the value of the variable which name contains, so I want it to output "20".

Any help here would be much appreciated.

Upvotes: 0

Views: 128

Answers (8)

Thomas Matthews
Thomas Matthews

Reputation: 57688

If you need to associate a name with a value, consider associative arrays otherwise known as dictionaries and maps. The Standard Template Library has std::map that you can use to associate text with a value:

#include <map>
#include <string>

std::map<std::string, int> my_map;

my_map["A"] = 20;

cout << my_map["A"] << endl;

Upvotes: 4

user477685
user477685

Reputation: 21

quick fix for outputting integers only:

int a; a = 20;

void getVar(int name) { cout << name; }

getVar(a);

If you need the function to work for any type of variable, maybe think about some template function.

Edit: Here is the code for the template program:

#include <iostream>
#include <string>

using namespace std;

template <class T>
void getVar(T name){
    cout<<name<<endl;
}

int main()
{
string x="hee";
int y=10;
getVar(x);//outputs hee
getVar(y);//outputs 10
return 0;
}

Upvotes: 0

Captain Giraffe
Captain Giraffe

Reputation: 14705

What you are trying to achieve is not possible in a compiled language (not considering reflection). You might accomplish something similar using a map data structure.

theMap["a"] = 20;

and a corresponding

void getVar(string key){
     cout << theMap[key];
}

that can be called with

getVar("a");

Note that in this extremely simple sample theMap has to be in scope for the function, like in a class or a namespace.

If you use pointers you are just using a level of indirection not at all suited for your example. See Chads answer for instance.

Upvotes: 2

Seva Alekseyev
Seva Alekseyev

Reputation: 61361

What you're asking for is called "reflection" or "introspection" - the ability to use design-time names for your program's objects (classes, variables, functions, etc) in run time. C++ does not support that out of the box - the design-time names are stripped upon compilation.

There are some libraries that provide that capability in C++; but there are also languages where reflection is is part of the language. Python or JavaScript, for example.

Upvotes: 1

atoMerz
atoMerz

Reputation: 7672

Your code does not use pointers. you're trying to convert a string into an identifier and print it's result, I don't know whether that's possible or not. If you intended using pointer your code should've looked like this:

int a = 20;
int* b = &a;
cout << *b;

Upvotes: 0

Benoit
Benoit

Reputation: 79185

Maybe this could suit you:

int a = 5;
class b {
public:
   b(int &x) { ref_ = x; }
   int operator()(void) { return ref_; }
private:
   int &ref_;
}

b my_b(a);
my_b() /* -> 5 */;

Upvotes: 0

Chad
Chad

Reputation: 19609

What you are thinking of is called (Reflection) which C++ does not support. You can however use pointers to access what is in a variable it points to:

int a = 5; //int variable that stores 5
int *b = &a; //int pointer that stores address of a

(*b) = 10; //stores 10 into address that b points to (a)

cout << a; //prints 10

Upvotes: 2

Dan F
Dan F

Reputation: 17732

Theres no real way for you to access variables by name like that unless you create some kind of container class that has a name member that you look up by. I'm not sure what this has to do with pointers though.

Upvotes: 1

Related Questions