This question already has answers here:
Function References
(5 answers)
Closed 8 years ago.
When to use function reference such as
void (&fr)() = foo;
fr();
instead of function pointer such as
void (*fp)() = &foo;
fp();
Is there something function pointer can't do but function reference can?
when you define a reference:
void (&fr)() = foo;
fr();
it gives you the ability to use fr almost everywhere where it would be possible to use foo, which is the reason why:
fr();
(*fr)();
works exactly the same way as using the foo directly:
foo();
(*foo)();
One more Difference is dereferencing a function reference doesn't result in an error where function pointer does not require dereferencing.
Related
This question already has answers here:
Ambiguous Reference/Value Versions of Functions
(4 answers)
Overload resolution between object, rvalue reference, const reference
(1 answer)
Ambiguous call with overloaded r-value reference function [duplicate]
(1 answer)
Closed 3 months ago.
Please refer to the below simple scenario:
void test(int k){
cout<<"Passed as value"<<endl;
cout<<k<<endl;
}
void test(int &k){
cout<<"Passed as reference"<<endl;
cout<<k<<endl;
}
int main(){
int x = 10;
test(x);
}
As far as my understanding is concerned, when you call a function, at that time the compiler has no way of knowing if the variable passed to it is by value or by reference. Thus, I understand why the compiler gives the error that call of overloaded ‘test(int&)’ is ambiguous. Is there any way that we can specify while calling the function itself that I intend to pass the value as reference?" N.B. I already know the work around this issue by passing the variable as address.
This question already has answers here:
What does void* mean and how to use it?
(10 answers)
Closed 1 year ago.
If void* makes a pointer that holds the address of any type, what does adding void* to the start of the function mean, why not just put void?
void* myFunction(void* arg)
{
int thread_id = *((int*) arg);
void *myFunction() means that myFunction returns "pointer to void", i.e. it returns a pointer, for which the type pointed to is not specified.
void myFunction() means that myFunction returns "void", i.e. it returns nothing at all.
This question already has answers here:
Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?
(5 answers)
How does dereferencing of a function pointer happen?
(5 answers)
Closed 9 years ago.
void f()
{}
void test()
{
auto fn_1 = f;
auto fn_2 = &f;
assert(fn_1 == fn_2); // OK
fn_1(); // OK
fn_2(); // OK
(*fn_1)(); // OK
(*fn_2)(); // OK
(**fn_1)(); // OK
(**fn_2)(); // OK
(***fn_1)(); // OK
(***fn_2)(); // OK
}
Are these behaviors explicitly defined by the C++ standard?
The issue at play here is that a function decays into a function pointer. The types of both of the variables fn_1 and fn_2 are void (*)(), i.e. "pointer to function taking no arguments and returning void". In the case of fn_1, the function f decays into a pointer to a function, while in the case of fn_2, you explicitly assign a pointer to a function to fn_2, and no decaying takes place.
Yes the ampersand is optional, they produce the same result.
An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.55
I'm just gonna go ahead and say if your using c++11 you should be using std::function anyway it's much easier to understand and use.
This question already has an answer here:
What is Node *& aNode? [duplicate]
(1 answer)
Closed 10 years ago.
I don't know much about C++ and I need to deal with a function at the moment. What does this mean in a function prototype?
void myFunc(int &size, signed char *&array);
This is a reference to a pointer. So you don't pass a copy of the pointer to the function, instead you pass a reference to it, which means any changes to that value in the function would actually affect the original pointer you passed as argument.
It means array is a reference to a signed char *, i.e. to pointer.
*& is a reference to a pointer
You suppose to call myFunc like this:
int size;
signed char *p;
myFunc(size, p);
Normally pass a reference to pointer to a function, so could change the pointer inside the function.
think of it in this way:
(int) &size;
(char*) &array;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing a pointer by reference in c++
I have a function that needs to modify a pointer, ex:
bool someFunc(Something* something)
{
something = somethingElse;
return true;
}
The pointer is passed by value through and is not modified. How can I modify it?
Thanks
Just change the function signature to look like
bool someFunc(Something* &something)
and you'll get a modifiable pointer in someFunc().
bool someFunc(Something * &something)
bool someFunc ( Something * & something );
// ^ notice the reference symbol