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
juicemongerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MeiaDoseCommented:
The &ar means the data adress of "ar".
0
bcladdCommented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

juicemongerAuthor Commented:
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
bcladdCommented:
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
bcladdCommented:
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
DexstarCommented:
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
himchCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dennis_georgeCommented:
Basically its a Judgement call  

 ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.