I want to call the same variable with a different name, how can I assign it an alias?
Do I stick to using a macro, like
#DEFINE Variable Alias
I'm currently doing it in C using functions as the method for renaming.
So given the variable: int signal
I'd do the following
int Temperature(){return signal;}
The way to provide an alias for a variable in C++ is to use references. For example,
int i = 42;
int& j = i; // j is an alias for i
const int& k = j; // k is an alias for i. You cannot modify i via k.
You say
int a, *b;
b = &a; // *b is now an alias of a
I like to think of a pointer as something that gives you a variable when you star it, and the ampersand as the alias-making operator.
An alternative to Eric Lippert's answer:
int a;
int * const b = &a;
Now *b
is the same as a
it can not be made to point to anything else, the compiler will not generate additional code to handle a
or *b
, neither reserve space for a pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With