Hey guys, I'm currently reading a book "Beggining C++ Through Game Programming 3rd Edition" and the author states that a reference is like an alias. Later in the book when he shows how to use pointers he states in a hint message that references should be used instead of constant pointers due to a cleaner syntax. My real question is are they the exact same thing, and if so what do you prefer. Honestly I hate syntax sugar because it is just another thing for someone to remember about a programming language. Also I read this article: An Insight to References in C++ - CodeProject, and it clearly shows that they are the same thing. However many replies were stating that references dont take up any space in memory like constant pointers. I know this is incorrect because it is impossible to have an alias in binary without storing.
5 replies to this topic
#1
Posted 16 November 2010 - 06:21 PM
|
|
|
#2
Posted 16 November 2010 - 06:33 PM
They are similar, but they are NOT the same thing. A reference is a variable that uses the same memory location as another (already existing) variable.
#3
Posted 16 November 2010 - 06:59 PM
The size of a pointer will tend to be 32 bits (4 bytes) assuming 32 bit platform of course, as that will allow it to point to all possible 32 bit memory addresses. References are only taking up space before the compiler (i.e. the physical & symbol), the compiler will resolve and connect the references automatically so it does not behave like a pointer.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#4
Posted 16 November 2010 - 09:03 PM
@NullW0rm The only thing that gets me is this code snippet.
From my understanding sizeof() is a unary operator that returns the size of the argument they are given. In this case each reference is returning 4, which I'm assuming means 4 bytes. Although I could be wrong because references are supposed to be initialized on creation and if this reference isn't being initialized at all it could be acting differently then a reference that is pointing somewhere.
Collapse | Copy Code
#include <iostream>
using namespace std;
class Test
{
int &i; // int *const i;
int &j; // int *const j;
int &k; // int *const k;
};
int main()
{
// This will print 12 i.e. size of 3 pointers
cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}
From my understanding sizeof() is a unary operator that returns the size of the argument they are given. In this case each reference is returning 4, which I'm assuming means 4 bytes. Although I could be wrong because references are supposed to be initialized on creation and if this reference isn't being initialized at all it could be acting differently then a reference that is pointing somewhere.
#5
Posted 16 November 2010 - 09:52 PM
Your reference is 0, your integer of the reference is 4 bytes. A reference does not exist in execution time and therefor has no existance. Knowing the reference the compiler can generate the exact same code when a reference is used just as when the thing it refers to is used.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#6
Posted 17 November 2010 - 06:44 AM
Okay, thanks for explaining that. I will try to use references whenever I can.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









