Reputation: 31
I am trying to make sure I understand the basic differences between some elementary pointer configurations. Would somebody mind explaining the differences between:
GenericTfIdfDocument &gd = d;
GenericTfIdfDocument gd = d;
GenericTfIdfDocument *gd = &d;
?
Any help would be greatly appreciated!
Upvotes: 2
Views: 72
Reputation: 1257
Sometimes code is clearer than words, so there are some code examples. I replaced int
instead of GenericTfIdfDocument
to make it easier.
Example 1
int d = 1;
int gd = d; //d is copied, gd and d are two different variables
d = 3; //setting d
gd = 2; // setting gd
Here d == 3
and gd == 2
. Both are different because one is a copy of the other.
Example 2
int d = 1;
int &gd = d; //gd is a reference of d, it works like an alias
d = 3; //setting d
gd = 2; // gd is a reference(like an alias), so this is the same as d = 2
Here d == 2
and gd == 2
. As gd
is a reference of d
then gd == 2
is the same as d == 2
.
Example 3
int d = 1;
int *gd = &d; //gd is a pointer to d. &d gives you the memory address of d
*gd = 2; //using a bare gd = 2 you're setting the memory address to 2 and we don't now which value is in address 2, in most of case is an invalid value, then you need to derrefence gd with *
Here d == 2
and *gd == 2
. *gd works similar to the reference case
Upvotes: 0
Reputation: 807
Starting with the most simplest:
GenericTfIdfDocument gd = d;
Here you are making an object of GenericTfIdfDocument called gd which stores the value d that gets stored somewhere on the stack. Hence, the variable gd is really just an alias for a memory location somewhere on the stack where the contents of d is stored (since it's a lot easier to use aliases as variable names instead of the actual memory location).
Next:
GenericTfIdfDocument *gd = &d;
Here, gd is declared as a pointer. The variable gd is now an alias that stores the memory location of the place on the stack which contains the value of d. If you attempt to print gd, it will display the memory location where the variable is stored, rather than the actual value of d. And so, if you need to access the variable you need to deference it with the * operator (*d) to tell the compiler to look at the contents of that memory cell that the variable is pointing to.
Lastly:
GenericTfIdfDocument &gd = d;
The variable gd is now a reference to the contents of d. The way the contents are stored work exactly like when you create a pointer. Except, references are easier to use and more readable in code since when you access the variable, you don't have to use the dereference operator and can instead just use the name directly (gd, not *gd). There are, however, some differences between these kind of references and pointers regarding re-assigning and the operations you can perform, etc.
Upvotes: 0
Reputation: 1847
1) GenericTfIdfDocument &gd = d; //gd is reference of d
2) GenericTfIdfDocument gd = d; // gd is copy of d...
3) GenericTfIdfDocument *gd = &d; //gd is pointer to d
Case1: if you try cout << &gd << &d; They will come out to be same as gd is just an alias for d and they both will have same memory address.
Case2: cout << &gd << &d; They both will print different address of memory as they both hold individual memory locations.
Case 3: cout << gd << &d; Interesting to see that gd is storing the address of d. So they both will output the memory address of d.
cout << &gd << &d; you will find that they both hold individual memory locations but gd value is equal to the address of the d. And as gd has same type as d, we can say gd is pointing to d as it stores it address.
If you do cout << *gd << d; then you will have the same output as gd is pointing to d and you did *gd which as same as saying print value of d.
Upvotes: 0
Reputation: 3112
If this is C++, then the following may help:
GenericTfIdfDocument &gd = d;
The above declares gd
to be a reference to a GenericTfIdfDocument
object type, and assigns gd
to reference the d
object.
GenericTfIdfDocument gd = d;
The above declares gd
to be a GenericTfIdfDocument, and copies the object d
into gd
.
GenericTfIdfDocument *gd = &d;
The above declares gd
to be a pointer to a GenericTfIdfDocument
object, and assigns gd
to point to the d
object. gd
in this case contains the address of d
.
Upvotes: 1