C#er
July 14th, 2008, 09:20 AM
Hi for all
I want to really understand pointers and references. I've read in wikipedia this:
1) It is not possible to refer to a reference object directly after it is defined; any occurrence of its name refers directly to the object it references.
2) As a consequence of the first point, neither arithmetic, casts, nor any other operation can be performed on references except copying their binding into other references.
3) Once a reference is created, it cannot be later made to reference another object; we say it cannot be reseated. This is often done with pointers.
4) References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.
5) References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.
I would like some explanations of some topics:
- The topic 1 I don't understand anything.
- In topic 2 it says that is not possible to cast a reference, but this code works fine in my compiler:
double d2 = 1.0;
double *d = &d2;
int it = NULL;
int& d1 = it;
f1 = static_cast<float>(d1);
d1 = int(d2);
The topic 4 is pointed that references cannot be null. In this simple code, am i not setting to null the reference var 'd1' indirectly?
The topic 5 says 'it's impossible to reinitialize a reference'. The end of my code doesn't this?
d1 = int(d2);
I'll be very grateful to answers to these questions. Any help is welcomed.
thanks
I want to really understand pointers and references. I've read in wikipedia this:
1) It is not possible to refer to a reference object directly after it is defined; any occurrence of its name refers directly to the object it references.
2) As a consequence of the first point, neither arithmetic, casts, nor any other operation can be performed on references except copying their binding into other references.
3) Once a reference is created, it cannot be later made to reference another object; we say it cannot be reseated. This is often done with pointers.
4) References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.
5) References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.
I would like some explanations of some topics:
- The topic 1 I don't understand anything.
- In topic 2 it says that is not possible to cast a reference, but this code works fine in my compiler:
double d2 = 1.0;
double *d = &d2;
int it = NULL;
int& d1 = it;
f1 = static_cast<float>(d1);
d1 = int(d2);
The topic 4 is pointed that references cannot be null. In this simple code, am i not setting to null the reference var 'd1' indirectly?
The topic 5 says 'it's impossible to reinitialize a reference'. The end of my code doesn't this?
d1 = int(d2);
I'll be very grateful to answers to these questions. Any help is welcomed.
thanks