Solved

Quick Pointer Question

Posted on 2003-10-29
9
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

726 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