Click to See Complete Forum and Search --> : References and pointers


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

Lindley
July 14th, 2008, 09:36 AM
Hi for all

I want to really understand pointers and references. I've read in wikipedia this:


I would like some explanations of some topics:
- The topic 1 I don't understand anything.


The below might make it clearer.


- 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);



No reason it shouldn't, but you may not be doing what you think you're doing. This is a fancy way of changing the value of it from 0 to 1.


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?


No. You're declaring an int with the value NULL (usually 0), and then declaring a reference to it. The reference itself is not NULL, only the value it refers to.


The topic 5 says 'it's impossible to reinitialize a reference'. The end of my code doesn't this?

d1 = int(d2);



No, you're changing the value of int it. The "value" of d1---a reference to it---remains the same.

exterminator
July 14th, 2008, 10:29 AM
I want to really understand pointers and references. I've read in wikipedia this:The key to understanding references is that they may or may not require memory. That is, they may or may not have an address. They are just aliases to objects.I would like some explanations of some topics:
- The topic 1 I don't understand anything.1 is as simple as it is stated. A reference is just an alias to the actual object. You declare a reference to some object and then use that alias. And everywhere that alias is used, consider it just the original object that it's aliasing. If you have an alias, another name for yourself and I use that alias to request you to do something, for example, "<your alternate name>, please sit on that chair", it is *you* who would respond. The alias is just another way to refer to you. Hope it makes sense. It isn't any simpler than that. - 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);As per 1, the cast is not on the reference but on the actual object that reference refers to. d1 is a reference, but the cast is actually being applied on "it". Not clear?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?d1 is an integer type. It cannot have a NULL value and the assignment to NULL that you have put in is actually 0 for the integer variable "it". So, d1 is a reference to the integer "it" that has a value 0. Having said that, it is possible to have a reference be NULL i.e. it refers to nothing but that is a violation of the standards and is illegal code.The topic 5 says 'it's impossible to reinitialize a reference'. The end of my code doesn't this?d1 = int(d2);What you effectively did is change the value of "it" to the value of d2 cast to an integer. You don't change the reference. Ever. It is not possible write a piece of code that does that using the C++ syntax.

Hope it helped and cleared the doubts. :)

JohnW@Wessex
July 14th, 2008, 10:44 AM
The topic 4 is pointed that references cannot be null.You can, sort off.
int *p = 0;

int &r = *p;

r = 1; // Kaboom!

Lindley
July 14th, 2008, 11:44 AM
Not legally....

C#er
July 14th, 2008, 12:09 PM
Understood Lindley, thanks for help.