int x, y;
int &ref = x;
int *ptr = &y
🔒 Locked
Pointers and References...
Advertisement
Original Post
From the C++ FAQ lite:
Quote:
Use references when you can, and pointers when you have to.
References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.
Generally speaking, it is best to use a reference when you don't wish to deal with the dereferencing of the pointer, and you don't need to change the address it is pointing to. For instance:
int &ref = somevariable;
ref++;
You don't need to go *ref++, it's more convenient, however, you cannot do:
ref = &someothervariable
If you ever wish to change the address that the variable is pointing to, you must use a standard pointer. I'm sure there are other slight differences involved, but I believe these are the major differences.
int &ref = somevariable;
ref++;
You don't need to go *ref++, it's more convenient, however, you cannot do:
ref = &someothervariable
If you ever wish to change the address that the variable is pointing to, you must use a standard pointer. I'm sure there are other slight differences involved, but I believe these are the major differences.
Quote:
Original post by Peregrin
From the C++ FAQ lite:Quote:
Use references when you can, and pointers when you have to.
References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.
what is "reseating"?
Sorry for my bad english...
Quote:
Original post by DMatovich
int &ref = somevariable;
ref++;
You don't need to go *ref++, it's more convenient
And more importantly, it would not be semantically equivalent!
int i = 5;int &x = i;x++; // increase istd::cout << i << std::endl; // outputs 6int i = 5;int *x = &i*x++; // dereference x, then increase x (!)std::cout << i << std::endl; // outputs 5What you want is:
int i = 5;int *x = &i(*x)++; // dereference x, then increase istd::cout << i << std::endl; // outputs 6BTW, is "dereference" the right term for what the * operator does? In german it's called "dereferenzieren".
Quote:
Original post by Fred304
BTW, is "dereference" the right term for what the * operator does?
Yes.
CM
im pretty much a noob at programming and when i think about pointers and references i think about scope. is the variable you are referencing intended to be changed where u created it?
Most of the time this could be no but as u advance using more object oriented design you find that are referencing and pointing to objects all the time . . im sure the use of these becomes more aparent as you increase in your skill
Most of the time this could be no but as u advance using more object oriented design you find that are referencing and pointing to objects all the time . . im sure the use of these becomes more aparent as you increase in your skill
References are in effect constant pointers (not to be confused with pointers to constants) which should never equal zero, with slightly different syntax, and the limitation that you can never create a pointer or reference to a reference (not that you would need to, better to store a copy of the reference instead). You can create a pointer or reference to the object being referenced all the same.
int variable = 7;int * const pointer1 = & variable;int & reference1 = variable;//equivilant (modifying the referenced object):*pointer1 = 3;reference1 = 3;//equivilant (creating more references to the referenced object):int * const pointer2 = pointer1;int * const pointer3 = & reference1;int & reference2 = * pointer1;int & reference3 = reference1;//no equivilant using reference1:int * const * const pointer_to_pointer = & pointer1; //pointer_to_pointer -> pointer1 -> variableint * const & reference_to_pointer = pointer1; //reference_to_pointer . pointer1 -> variable//int & & reference_to_reference = reference1; //../main.cc:19: error: cannot declare reference to `int&'//int & * const pointer_to_reference = & reference1; //../main.cc:20: error: cannot declare pointer to `int&'
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement