Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alias variables vs references

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
0
0
NL
hi,
i have just started out with c++. unfortunately the material that i'm reading does not explain the difference between what it calls alias variables & variable references.
hope somebody knows.

thx.
 
A reference is an alias.

If I have the following code:

Code:
int y;
int &x = y;  // x is a reference to or alias of y

Now anything that I do to x (like x=10) is really done to y. x (which is a reference variable) is just an alias for y. Of course you've probably googled to find this info by now, looks like you've waited awhile for a response.
 
yeah, the information i found always pointed to the fact that aliases are the same as references. I prefer the name alias though.

 
Actually if you look closely, they are not the same. I said a reference is an alias, I didn't say an alias is a reference. A reference variable is an alias to an actual variable, but an alias is not a reference variable.

Alias just means a different name for the same thing. You can create aliases to variables in other ways than by using a reference variable. You can also create an alias to virtually anything, including classes, methods, namespaces, arrays, structures, objects etc., all without the use of reference variables.

Hope that makes sense.
 
I would say it this way: the term used in C++ books, help etc for "int &x = y;" thing is "reference".
You better use correct term - so you will understand what's going on and people would understand what you trying to say.
 
If an alias isn't a reference variable, then how do i declare an alias?
 
> If an alias isn't a reference variable, then how do i declare an alias?

You're making the concept of an alias more difficult than it is. There is no strict definition of an alias in C++, an alias is a general concept, not a specifically defined type. In the case of a reference, you have 2 pointers (references are really just pointers) that have different names, but that point to the same memory location. In the strictest sense, aliases are different pointers that point to the same memory location, but in the general sense, an alias is just another name for something.

You should be familiar with the concept of an alias in everyday life: I have a buddy named Bartholomew, I call him Bart, but everyone knows that when I say Bart, I mean Bartholomew. Bart is an alias, you could also call it a nickname.

Say I have something such as a namespace that has a very long name and I don't want to type the name of the namespace every time, I could create an alias, (or nickname) for the namespace that is much shorter:

Code:
namespace A_VERY_LONG_NAMESPACE_NAME {
  void SomeFunc(void);
}

namespace AVLNN = A_VERY_LONG_NAMESPACE_NAME;
// I could also do this:
#define AVLNN A_VERY_LONG_NAMESPACE_NAME

Now, whether I use A_VERY_LONG_NAMESPACE_NAME or the alias (nickname) AVLNN, in my code, it doesn't matter, the compiler knows what I mean (well sort of, one of the things that most optimizing compilers do is alias analyisis to try and identify and optimize aliases).

How about an integer variable:

Code:
int My_Very_Long_Integer_Variable_Name;

// I can define an alias like this:
#define MyInt1 My_Very_Long_Integer_Variable_Name

// I can also define an alias like this:
int &MyInt2 = My_Very_Long_Integer_Variable_Name;

Now, whether I use My_Very_Long_Integer_Variable_Name, MyInt1 or MyInt2 in my code, it's the same, each represents the same variable.

I could do something similar with arrays (or classes or structures...) and pointers:
Code:
  char *myCharArray[MYMAXLENGTH];
  char *mca = myCharArray;

Basically, any way that I can create another name for something gives me an alias.

Does that make sense?
 
I understand now that reference variables can also be referred to as aliases, thanks.
There's just one thing:
Code:
int My_Very_Long_Integer_Variable_Name;

// I can define an alias like this:
#define MyInt1 My_Very_Long_Integer_Variable_Name

// I can also define an alias like this:
int &MyInt2 = My_Very_Long_Integer_Variable_Name;
They may be used the same, but internally (at the machine code level) they are very different. The symbolic constant is substituted with the actual integer address at compile time, whereas the reference variable is used as a pointer to the integer.
 
They may be used the same, but internally (at the machine code level) they are very different. The symbolic constant is substituted with the actual integer address at compile time, whereas the reference variable is used as a pointer to the integer.

That is true, sometimes (one of you compiler gurus correct me if I'm wrong). If you have a compiler that performs good alias analysis, in the examples above both the reference and symbolic constant would ultimately be handled identically. The only difference is when each is replaced by the actual variable at compile time. The constant is replaced during the first pass, and the reference some time later. Remember, a reference is just a pointer that points to the memory address of the actual variable. If the compiler can, it will optimize away the reference and the machine code for both will be identical.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top