URGH! Pointers vs References... constants?!

Started by
3 comments, last by Emonious 20 years, 4 months ago
Okies... still weeding through my book on C++. Things are getting quite messy now, as the books ability to explain is taking quite a dive. Right now I''ve covered Pointers, and last chapter References (fairly simple enough, but still leary if they really ARE a form of pointer!). However, now in the next chapter it''s thrown them all together, and comparing them and afterwhich is speaks of constant pointers to pointer constants or something as some sort of security form to protect a class from tampering. Let''s just say I''m not done with the chapter and I''ve gotten quite lost, maybe this happened a few chapters ago after it started talking about using pointers to classes. Either way, is there anyone out there who can explain the idea of pointers vs references clearly? Maybe give me some insight to this jargon of constant pointers as a security option is? Thanks to any who attempt the feat
-----------------------------Naze ga muzukashi desu ka...
Advertisement
Here''s some comparison between pointers and references:
int a = 5;int b = 2;//there two are basically the same:int * const ptr = &a;int & ref = a;ptr = &b; //error. can''t change ptr to point to somewhere else, because "const" appeared after * in the definition*ptr = b; //ok. changes value of the variable where pts points at (i.e. ''a'')ref = b; //same as above//e.g. these two would also be equal statements: &ref  <=>  &(*ptr)//these two are basically the same:const int * const ptr2 = &a;const int & ref2 = a;ptr2 = &b; //error. can''t change ptr to point to somewhere else*ptr2 = b; //error. can''t change the value of where ptr points at, because "const" appeared before * in the definitionref2 = b; //same as above (error) 


Notice that there''s no syntax for changing a reference to reference at some other variable. It''s assignment is always an assignment to the variable where the reference points at, i.e. same as *x = b; where x is a pointer. References are sometimes called auto-dereferencing pointers, since the only difference between them and pointers seems to be the missing *-character when accessing the reference.
Underneath, a reference is really a pointer, it''s just that the language hides all the dirty details from you. The core idea is this - the storage space for pointer and reference variables is used solely to hold the location of the actual variable you are working with. When this is a pointer, you have the raw memory location and you use special language features to do things on the object it points to. References are similar, only they allow you to pretend you are actually working on the real instance itself by hiding the fact it''s a pointer, so you can have a bunch of different identifiers that all refer to the same instance - similar to object types in Java, which are inherently references.
You cannot change what a reference refers to after initialization as opposed to pointers;

int* ptr;//Ok
int& ref;//Error must refer to something

int INT1 = 5;
int INT2 = 10;

int* ptr = &INT1// ptr points to INT1;
ptr = &INT2// ptr points ti INT2;

int& ref = INT1;
ref = INT2;// ref does not refer to INT2. INT1 was assigned the value of INT2

Peace Out!
from gotw (a good resource to chew on)
Null References

This topic is closed to new replies.

Advertisement