diference definition of function

Jun 19, 2012 at 4:19pm
I have this prototype :

void foo (int *& x);
void foo (int & x);


What is the difference between defining functions like this?

Which is more convenient?
etc..
Jun 19, 2012 at 4:42pm
They are not the same, and nobody would use the first one.

int *&: Means "a reference to a pointer to an integer".
int &: Means "a reference to an integer".

If the first one were void food(int *x), then:

int *: Means "a pointer to an integer".

Which one you use? In parameters makes little difference except that the concept of a references only exists for C++. If you are programming C, such a concept doesn't exist.

So in general you would use references if you need to pass something by reference because the function wants to change it or because you don't want to create a copy for performance issues.

You would use pointers for the same reasons that you would use references. Just remember that pointers are the ONLY option for C, while in C++ you have a choice between pointer and reference.

You would also pass along a pointer if you NEED a pointer. For instance, construction of a class that requires an instance of some other class to be used, say, as a helper object or to implement member methods.
Jun 19, 2012 at 4:57pm
closed account (zb0S216C)
webJose wrote:
"They are not the same, and nobody would use the first one."

I use them occasionally. What if I wanted to work with the pointer itself, and not the object?

Wazzak
Last edited on Jun 19, 2012 at 4:57pm
Jun 19, 2012 at 5:03pm
Framework, you are nobody ;)
Jun 19, 2012 at 5:19pm

@gonza8888

I have this prototype :

void foo (int *& x);
void foo (int & x);


What is the difference between defining functions like this?


Speaking directly the difference in the types of the parameter. In the first declaration the type of the parameter is a reference to a pointer to int. And in the second declaration the type of the parameter is a reference to an int object.

Remove the reference and compare

void foo (int * x);
void foo (int x);
Jun 19, 2012 at 7:07pm
hehe, Framework: I cannot be held responsible for what Peter said. Don't be mad at me. :-P

Having said that, it is said (as I have never checked myself) that passing primitive types by reference is actually less performant than copying them. Pointers are primitive types, therefore passing them by reference is less performant and not recommended unless you intend to make them point elsewhere.

So yes, I guess I stand corrected: People would only use the first one if there is intent to change the pointer's address.
Jun 19, 2012 at 7:46pm
closed account (zb0S216C)
Peter87 wrote:
"Framework, you are nobody ;)"

I'm Nobody. I'm Anonymous. I'm Legion. I do not forgive. I do not forget. Expect Me.

Wazzak
Last edited on Jun 19, 2012 at 7:48pm
Jun 19, 2012 at 7:52pm
I'm Nobody. I'm Anonymous. I'm Legion.


Are you spoonlicker?
Jun 19, 2012 at 9:36pm
closed account (zb0S216C)
@Moschops: Yeah.

Wazzak
Topic archived. No new replies allowed.