|
-
July 14th, 2008, 09:20 AM
#1
References and pointers
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:
Code:
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?
I'll be very grateful to answers to these questions. Any help is welcomed.
thanks
-
July 14th, 2008, 09:36 AM
#2
Re: References and pointers
 Originally Posted by C#er
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:
Code:
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?
No, you're changing the value of int it. The "value" of d1---a reference to it---remains the same.
-
July 14th, 2008, 10:29 AM
#3
Re: References and pointers
 Originally Posted by C#er
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.
 Originally Posted by C#er
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.
 Originally Posted by C#er
- In topic 2 it says that is not possible to cast a reference, but this code works fine in my compiler:
Code:
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?
 Originally Posted by C#er
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.
 Originally Posted by C#er
The topic 5 says 'it's impossible to reinitialize a reference'. The end of my code doesn't this?
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 14th, 2008, 10:44 AM
#4
Re: References and pointers
 Originally Posted by C#er
The topic 4 is pointed that references cannot be null.
You can, sort off.
Code:
int *p = 0;
int &r = *p;
r = 1; // Kaboom!
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
July 14th, 2008, 11:44 AM
#5
Re: References and pointers
[Willow]Not legally....[/Willow]
-
July 14th, 2008, 12:09 PM
#6
Re: References and pointers
Understood Lindley, thanks for help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|