When dereferencing pointers does it create a whole new object for you to work with or does it refer back to the original object without creating a new object?
When dereferencing pointers does it create a whole new object for you to work with or does it refer back to the original object without creating a new object?
It refers back to the original object.
Okay, I'm going to assume when you say refers that you mean something like this:
int i;
int& j = i;
In which case it will be a high performance thing when deferencing big classes and structs. Thakns.
That code uses a reference, not a pointer. You can almost always interchange them though -- except for the few things pointers can do that references can't.
ok ya that was a bad example. What I ment was dereferencing a pointer is like doing this:
int i = 5;
int& j = i; //int j = *i would be equivilent to this line of code right?
Edit: Sorry for the double post, I am really having a hard time thinking today.
Ok I think I got it all figured out. I just had to think and do a little testing, my brain had a hard start this morning.
Code:int i = 5; int* p = &i; int j = *p; j = 7; //j equals 7 but i equals 5 (it copied the contents i to j) int& q = *i; q = 7 //q points to i, i now equals 7
Last edited by Rune Hunter; 07-13-2007 at 09:52 AM.
Yes, you've got it right. Dereferencing a pointer returns a reference, but if you assign that to a non-reference variable then a copy occurs.
Alright perfect! Now that I got confirmation it all makes sence.
Thanks everyone!
Not so. Pointers may be different sizes, even between different types, not to mention between different architectures. Granted, having sizeof(type1 *) != sizeof(type2 *) is rare, but it's allowed by the standard (the C standard anyway, I'm assuming that the C++ one is of the same mind).
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I think its a given that between different platforms, then all bets are off for the size of any type (including the number of bits in a byte)
Although within the same platform, I wonder, how would the situation be handled, when casting between void* and T* where sizeof(T*) != sizeof(void*) ?
I was also under the impression that there was a relationship between the size of the address bus, and the size of a pointer (within any given platform), which would make it somewhat nonsensical to have varying pointer sizes. (unless perhaps the platform for some reason is dealing directly with more than one area of addressable memory)