juicemonger
asked on
Quick Pointer Question
Generally I think I understand pointers, but there's one thing that still throws me:
void Serialize(CArchive &ar);
What does this kind of declaration mean? Is it looking for a CArchive object, pointer, or what? I'd understand (CArchive* ar) or (CArchive ar), but... huh?
I know I'm using MFC examples, but a pointer question seemed general enough for the C++ section...
Thanks
void Serialize(CArchive &ar);
What does this kind of declaration mean? Is it looking for a CArchive object, pointer, or what? I'd understand (CArchive* ar) or (CArchive ar), but... huh?
I know I'm using MFC examples, but a pointer question seemed general enough for the C++ section...
Thanks
The &ar means the data adress of "ar".
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This means that the parameter is passed by reference.
http://www.fredosaurus.com/notes-cpp/functions/refparams.html
http://www.infocom.cqu.edu.au/Staff/Mike_Turnbull/Home_Page/Lecturetts/Sect5.htm
http://appsrv.cse.cuhk.edu.hk/~csc4510/cxx/tutorial.3/5.htm
--efn
http://www.fredosaurus.com/notes-cpp/functions/refparams.html
http://www.infocom.cqu.edu.au/Staff/Mike_Turnbull/Home_Page/Lecturetts/Sect5.htm
http://appsrv.cse.cuhk.edu.hk/~csc4510/cxx/tutorial.3/5.htm
--efn
ASKER
Well, that certainly makes sense... but...
How does one decide to pass by reference rather than pass a pointer? When passing something that's already a pointer, like a char*, it seems like a straightforward issue, but why declare and call:
void a_function(int &aNumber);
int x;
a_function(x);
rather than:
void a_function(int *aNumber);
int x;
a_function(&x);
Is one better in certain situations than the other? --As far as I can tell, it just means the function will use a bunch of arrows instead of periods.
How does one decide to pass by reference rather than pass a pointer? When passing something that's already a pointer, like a char*, it seems like a straightforward issue, but why declare and call:
void a_function(int &aNumber);
int x;
a_function(x);
rather than:
void a_function(int *aNumber);
int x;
a_function(&x);
Is one better in certain situations than the other? --As far as I can tell, it just means the function will use a bunch of arrows instead of periods.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The reason for prefering reference over pointer is ease of use for the CALLER. It is annoying to have to specify the address operator in every call when the reference can be specified once in the declaration of the function. It is possible to forget the address operator and, in old C compilers, get your code to compile with devistating results (it is unlikely that after x=3 that x contains a valid integer variable address).
Another reason for using references is that you can pass const references, something much harder to specify with pointers. A const reference is passed by reference but the function cannot modify the local reference. This permits large objects to be passed as "pointers" for efficiency but treated almost as "values" semantically.
Pointers have their uses (dynamically allocated memory, C-style strings, etc.) but references are often times easier to understand from the caller's perspective. Adding const references greatly increases the documentation value of parameter types to functions.
Another reason for using references is that you can pass const references, something much harder to specify with pointers. A const reference is passed by reference but the function cannot modify the local reference. This permits large objects to be passed as "pointers" for efficiency but treated almost as "values" semantically.
Pointers have their uses (dynamically allocated memory, C-style strings, etc.) but references are often times easier to understand from the caller's perspective. Adding const references greatly increases the documentation value of parameter types to functions.
juicemonger:
> Is one better in certain situations than the other? --As far as I can tell,
> it just means the function will use a bunch of arrows instead of periods.
Like others have explained, its a C++ style thing to add "pass by reference" to C, which typically only had "pass by value" and thus required pointers.
An important thing to know is that when you compile, the compiler will basically translate:
void SomeFunc( int& nValue )
To:
void SomeFunc( int* nValue )
So, under the hood, they are the same.
Hope that helps,
Dex*
> Is one better in certain situations than the other? --As far as I can tell,
> it just means the function will use a bunch of arrows instead of periods.
Like others have explained, its a C++ style thing to add "pass by reference" to C, which typically only had "pass by value" and thus required pointers.
An important thing to know is that when you compile, the compiler will basically translate:
void SomeFunc( int& nValue )
To:
void SomeFunc( int* nValue )
So, under the hood, they are the same.
Hope that helps,
Dex*
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Basically its a Judgement call
;-)
;-)