|
|
000A1000 push ebp 000A1001 mov ebp,esp 000A1003 push ecx 000A1004 mov ecx,dword ptr [__imp_std::cin (0A2048h)] 000A100A lea eax,[i] 000A100D push eax 000A100E call dword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (0A2044h)] 000A1014 xor eax,eax 000A1016 mov esp,ebp 000A1018 pop ebp 000A1019 ret |
Assuming there is no compiler optimization, wouldn't references be implementation defined? |
good job trolling me. |
I wrote: |
---|
...that a compiler doesn't have to provide storage for a reference if it can optimised away |
Can you give me an example where a reference does not require storage and an equivalent pointer does? |
When used like a reference, pointer is treated like a reference. |
mportant note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object. |
and an equivalent pointer does? |
Surely you must agree that "functionality of a reference is a subset of functionality of a pointer", right? |
Stroustrup wrote: |
---|
More generally, if you want to have both the functionality of pointers and the functionality of references, you need either two different types (as in C++) or two different sets of operations on a single type. From 'Why does C++ have both pointers and references?' in http://www2.research.att.com/~bs/bs_faq2.html |
Important note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object. |
Strange phrases like "a reference IS the object" are used quite frequently in the C++ community. Such claims are only useful to hide the fact that C++ pointers & references are so similar that having both in the language is an unnecessary complication. In other contexts, the claims are simply false. For example, a wide class of bugs comes from accessing dangling references - references to objects which were already destroyed. If a reference is the object, or just another name for it, how can that happen? Names of destroyed objects are inaccessible - it takes a previously assigned pointer to access a destroyed object (C++ also breaks that rule - you can access a destroyed global object by its name from a destructor of another global object, but that's a different can of worms). |
Now you agree that all references are really pointers, but deny the opposite, which was never even claimed. |
"When used like a reference, pointer is treated like a reference" means that you write a piece of code using int& ref and then change the declaration to int* ptr and all other occurrences of ref to (*ptr). Is it really not intuitive? |
access and modify an object without using its declared name |
FQA wrote: |
---|
the claims are simply false |
I genuinely do not consider a reference and a pointer to be the same thing |
I'm fine with that. |
old thing + new syntax + some restrictions = an entirely new thing. |
What is it that you can do to a reference but not to a pointer? |
hamsterman wrote: |
---|
For Stroustrup, I'd say the quote is not well picked. Functionality is an ambiguous word. |
hamsterman wrote: |
---|
Let me paraphrase it the last time: functionality of a reference is a subset of functionality of a pointer. |
a wide class of bugs comes from accessing dangling references - references to objects which were already destroyed. If a reference is the object, or just another name for it, how can that happen? |