-
November 14th, 2016, 03:42 AM
#1
The difference between "alias" and "reference" in C++
Hello all,
Would you please say the difference between an alias and a reference in C++?
I think they are the same.
for example:
Here, b is an alias for the existing variable a (the type is integer).
And in here:
Code:
vector<double>* get_from_jill();
vector<double>* jill_data = get_from_jill();
vector<double>& v = *jill_data;
Here, v is an alias for the existing variable *jill_data (the type is *jill_data)
Would you please say your opinion?
Do you agree that an alias and a reference are the same?
-
November 14th, 2016, 04:16 AM
#2
Re: The difference between "alias" and "reference" in C++
in programming terminology, 'aliasing' refers to the ability of different expressions of referring to the same object.
So, a reference is an alias, but not all aliases ( = aliasing expressions ) must be references, pointers being the most obvious example.
-
November 16th, 2016, 03:06 AM
#3
Re: The difference between "alias" and "reference" in C++
Do you by your explanation mean, for example:
int n = 5;
int* p1 = &n;
int* p2 = p1;
Here although p1 is an alias for p2 but it is not a reference?
-
November 16th, 2016, 04:10 AM
#4
Re: The difference between "alias" and "reference" in C++
p2 is an alias for p1 as both point to the same memory address. However if either p1 or p2 is changed then p2 will no longer be an alias as p1 and p2 would point to different addresses.
Another area of alias in c++ that is not a reference is in type definition. Consider
Code:
typedef vector<int> vi;
here vi is an alias for the type vector<int> but vi obviously isn't an alias. Personally in c+ I don't like the term alias when referring to variables only when referring to types.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 16th, 2016, 06:00 AM
#5
Re: The difference between "alias" and "reference" in C++
tomy, rather than focusing on terminology ( as 2kaud said, 'alias' is seldom used with respect to variables; as I already said, 'aliasing' may be a more useful term for indicating the phenomenon of aliasing and its implications on code correctness/performance )
you shoud try explaining what your doubts are concerning who aliases who.
in your example, p1 does not alias p2 ( being different objects ) but they both alias n via pointer indirection.
-
November 16th, 2016, 08:22 AM
#6
Re: The difference between "alias" and "reference" in C++
Originally Posted by tomy12
Do you by your explanation mean, for example:
int n = 5;
int* p1 = &n;
int* p2 = p1;
Here although p1 is an alias for p2 but it is not a reference?
Further to my comment in post #4 and looking again at this, IMO neither p1 nor p2 can be considered alias as both p1 and p2 can be changed. For p1 (and p2) to be an alias of the address of n, I would consider
Code:
int n = 10;
int* const p1 = &n;
int* const p2 = p1;
where p1 and p2 are const pointers that cannot be changed so always point to the address of n. Hence where &n is used then either p1 or p2 could be used as p1 and p2 cannot be changed.
Last edited by 2kaud; November 16th, 2016 at 12:54 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 16th, 2016, 12:55 PM
#7
Re: The difference between "alias" and "reference" in C++
But in terms of variables, IMO the term alias wouldn't be used in c++. Consider
Code:
int* const &p3 = &n;
auto const &p4 = &n;
In this case p3 and p4 are references.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|