Solved

Quick Pointer Question

Posted on 2003-10-29
9
221 Views
Last Modified: 2011-04-14
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
0
Comment
Question by:juicemonger
9 Comments
 
LVL 2

Expert Comment

by:MeiaDose
ID: 9645997
The &ar means the data adress of "ar".
0
 
LVL 11

Assisted Solution

by:bcladd
bcladd earned 50 total points
ID: 9646027
MeiaDose is  wrong about the meaing of & in this context.

When declaring a function in C++, & before a parameter means that the parameter is passed by reference. Syntactically this means it looks like a normal, pass by value, variable. Assuming CArchive has a Release() member function, inside your Serialize it could be called with

    ar.Release();

(no arrow, just the dot).

The difference with pass by reference from pass by value is that anything that happens to ar inside the function happens to the actual parameter, too. So if I call with

    CArchive z;
    Serialize(z);

and inside of Serialize I set ar's thingyCount member to 7, z's thingyCount is also 7 after the call. That is because ar REFERS to z.

As you might have guessed, UNDER THE HOOD, references use pointers so that ar and z refer to the same spot in memory.

Hope this helps, -bcl
0
 
LVL 15

Expert Comment

by:efn
ID: 9646029
0
 

Author Comment

by:juicemonger
ID: 9646724
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Assisted Solution

by:bcladd
bcladd earned 50 total points
ID: 9646839
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.

0
 
LVL 11

Expert Comment

by:bcladd
ID: 9646840
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.

0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9646982
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*
0
 

Accepted Solution

by:
himch earned 30 total points
ID: 9648706
Pointers and references look different enough (pointers use the "*" and "->" operators, references use "."), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly.

First, there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must always refer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.

char *pc = 0;          // set pointer to null

char& rc = *pc;        // make reference refer to

Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized:

string s1("Hello");
string s2("World");

string& rs = s1;         // rs refers to s1

string *ps = &s1;        // ps points to s1

rs = s2;                 // rs still refers to s1,
                         // but s1's value is now
                         // "World"

ps = &s2;                // ps now points to s2;
                         // s1 is unchangedIn general, you should use a pointer whenever you need to take into account the possibility that there's nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points).

NOTE:  You should use a reference whenever you know there will always be an object to refer to and you also know that once you're referring to that object, you'll never want to refer to anything else.

There is one other situation in which you should use a reference, and that's when you're implementing certain operators like [].

* NOTE:  References, then, are the feature of choice when you know you have something to refer to, when you'll never want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with pointers.



Himanshu

0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9648715
Basically its a Judgement call  

 ;-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now