Join us at IRC!
Imagination is more valuable than knowledge - Albert Einstein
Wednesday, May 23, 2012
Navigation
Members Online
Total Online: 21
Web Spiders: 15
Guests Online: 21
Members Online: 0

Registered Members: 70170
Newest Member: bahmx
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ function declaration argument handling

Zkunxen
Member

Posts: 47
Location:
Joined: 24.07.09
Rank:
Elite
Posted on 17-09-09 15:29
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?
Author

RE: C++ function declaration argument handling

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 17-09-09 15:57
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.




ynori7 http://halls-of-valhalla.org
Author

RE: C++ function declaration argument handling

Zkunxen
Member

Posts: 47
Location:
Joined: 24.07.09
Rank:
Elite
Posted on 17-09-09 16:13
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.

Edited by Zkunxen on 17-09-09 17:00
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.