Archived

This topic is now archived and is closed to further replies.

URGH! Pointers vs References... constants?!

This topic is 5133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 definition
ref2 = 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites