function parameters/reference?

G

Guest

What is the difference between

void MyFunction(int& param1){...
void MyFunction(const int& param1){...
void MyFunction(const int param1){...

Can you also use the 'reference to' operator with handles (and them still work
lik
void MyFunction(const HINSTANCE& hInst){...}
 
C

Carl Daniel [VC++ MVP]

Bonj said:
What is the difference between:

void MyFunction(int& param1){...}

reference to int
void MyFunction(const int& param1){...}

reference to const int.
void MyFunction(const int param1){...}

const copy of int.

Let's generalize just a bit:

void f(T& param) // 1
void f(const T& param) // 2
void f(const T param) //3

In 1 and 2 'param' is not (usually) copied - rather, f operates directly on
the variable that was used where f was called. In 1, modifications to
'param' from within 'f' will modify the variable that was used in the call,
in 2 modifications to 'param' are illegal.

In 3, 'param' is a copy of the actual argument to f at the call site, but f
is prohibited from modifying that copy. This prohibition is basically
pointless since f alone has access to the copy. There might be cases where
this makes sence if 'T' instances hold pointers to other objects that are
shared between the instances of 'T'.
Can you also use the 'reference to' operator with handles (and them
still work) like
void MyFunction(const HINSTANCE& hInst){...}

Of course. handles are nothing special - they're just typedefs to void*.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top