Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ function declaration argument handling


ghost's Avatar
0 0

I'm a bit confused about a class method I'm using in a class template. the method was declared as so:

...
double getSlope(point& n)
...

so I assumed I would then need to pass a point by reference to the method:

double d=b.getSlope(&a);
//but when I call it like that, I get the following errors:
/*
error: no matching function for call to 'point<double>::middle(point<double>*)'
note: candidates are: point<Tn> point<Tn>::middle(point<Tn>&) [with Tn = double]
*/

without the ampersand, however, it compiles and runs properly. would it be more correct like this:

...
double getSlope(point n)...
//and then call the method like this:
double d=b.getSlope(&a);

or does it still pass by reference because it was declared to pass by reference? or am I just completely not understanding something?


ynori7's Avatar
Future Emperor of Earth
0 0

Do you have some need for pointers? Because if you don't, it's easier to not use them.

And you're best way to figure out what's going wrong would be to read a few explanations about how pointers work and pointer arithmetic.


ghost's Avatar
0 0

I may still be a bit confused, but isn't it a bit faster to pass the address of an object, rather that just use the object's identifier as the argument, and have the entire value of the object copied to the function? it may not make much of a difference in my program, so I guess it really doesn't matter. I guess what I'm really asking is would this:

int someFunction(object& a){
...}
someFunction(a);
//be, essentially, the same as this:
int someFunction(object a){
..}
someFunction(&a);

Edit: nevermind, I found my answer here: http://www.linuxquestions.org/questions/programming-9/c-passing-a-variable-by-reference-to-a-class-member-function.-383445/ and they are the same for my purposes.