Reference and dereference operators

Hi there,
I am sort of confused by reference and dereference operators in c++.
The more I study the more I confused.
as far as I understood & is a memory address.
* is called both a pointer and dereference operator which refers to the value stored in a specific memory address. e.g. * k is referef to the value stored in k
Could any one tell if I am right or wrong?

I am wondering also about this statement I got from some tutorials: ''references should be initialized as soon as they are created''
what does initialization of a memory address means!!! if ted= & andy and the memory address of andy is 1776, what is the initialization of this reference means
Thank you in advance
*k is referef to the value stored in k

No. That is wrong.

If k is a pointer, then k has a value. k's value is a number. That number is a memory address, and is generally the address of some other variable somewhere in the memory. k is a complete object that takes up space in memory and has its own value.

*k is the value in that other memory address; that is, the value of that other variable.
Last edited on
Thank you for your help . could you please also tell me what does this type of writing mean:

G4RunManager * runManager = new G4RunManager;

I think that "runmanager" is a variable in the class "G4RunManager" which sets to be equal to another variable that is new G4RunManager;

Thank you again
It means that a pointer is set up:
G4RunManager * runManager;
This pointer will have to refer to a type called G4RunManager. (G4RunManager is any previously set-up type (struct, class, or the like). Probably it's set up, as a class you mentioned, earlier in the code you have.)
And then a value (which is the number of an address that the pointer points to) is put into this pointer:
runManager = new G4RunManager;
The value that is put into the pointer runManager is the addres of a new piece of memory that has the type G4RunManager. Thus, the keyword new, which is one of the few standard keywords in c++, means that there should be a new space of memory. At the same time it "returns" the address of that space in memory. That is why it is possible to put the address of this new piece of memory directly in a pointer.
Then, this pointer can be used to change things in the new part of memory (with type G4RunManager) through the pointer identified as runManager.
Tutorial on new: http://www.youtube.com/watch?v=TWDgMdsLAaU&feature=related
See: http://msdn.microsoft.com/en-us/library/kewsb8ba(v=vs.71).aspx .
Last edited on
So can I say that if G4Runmanager is an ABC, I make a copy of it in new G4RunManager and points to it????
I don't know what an ABC is, but...
You could sort of make a copy in new G4RunManager, but only through the pointer. There is no other way to access the new memory space than through the pointer.
Thank you for your descriptive answer.
By ABC I mean an abstract base class. so am I right to say that in this new memory space a copy of that class is created??
I don't think that's necessary. Someone else might be able to help. Anyway, you already have a (global) abstract class. Why make an instance/member of it?
Last edited on
Thank you very much for your help so far. I am new and it seems that I am confused. This is my last question.
I read the tutorial for pointers in c++ in www.cplusplus.com.

---------------------------------------
andy=25;
ted=& andy; e.g.(1716)
beth= *ted;

So in this case ted refers to the memory address of andy e.g:1716 and beth is equal to the value pointed by ted which is (25)

I really appreciate if you answer my last question. So in the case
G4RunManager * runManager = new G4RunManager;

I feel that the value that is pointed by runmanager is stored in the new mwmory location.....did I understand correctly?
Yes, almost.
ted refers to the memory address of andy

ted is the memory address of andy. The value stored in a pointer is an address. But I think you are getting the point.
beth is equal to the value pointed by ted

Now you get it right. Indeed ted points/refers to the value stored in andy. By using the dereference operator * it is the value referred/pointed to by ted (which itself is the address of andy) that is put into beth.
the value that is pointed to by runmanager is stored in the new memory location

You're right, except that there is no value in the new memory location, yet. Using new is like using int in that you (can) first "declare":
1
2
int x;
int * ptr = new int;

and then use them:
1
2
x = 5;
*ptr = x;
Last edited on
first of all, please note that both & and * are overloaded in C++

& can mean address of something or reference to something (depending on the context)
* can mean the dereference operator or multiplication in arithmetic

NOTES:
1. & meaning the address of something has nothing to do with references
2. * and & for pointers and address are "inverse operators" of each other, in the same sense that + and - are inverse operations or * and / are inverse operations: when you take the address of something by using &, you later get back what it's pointed to by using dereference or *

also note that * and & for pointers and addresses came from C and is available in C++
& as a reference is a C++ construct (but references functionality can be had in C by using pointers)
Thank you methodos, you are really great in explaining:)

Thanks to others as well:)
Topic archived. No new replies allowed.