Solved

Quick Pointer Question

Posted on 2003-10-29
9
241 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 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