#### Archived

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

# URGH! Pointers vs References... constants?!

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

## 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 on other sites
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.

##### 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 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 on other sites
from gotw (a good resource to chew on)
Null References

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009491
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!