This question already has answers here:
How can I pass a member function where a free function is expected?
(9 answers)
How can I pass a member function pointer into a function that takes a regular function pointer?
(4 answers)
Closed 9 months ago.
I'm having problems casting a function pointer.
It works (no cast needed) outside a class.
Here is the signature of the function I'm calling.
Result* FancyClass::callMe(void(*functionPointer)())
It works with.
void defaultState()
{
//
}
// ..
Result *result= instance.callMe(defaultState);
But it does not work with
void MyClass::defaultState()
{
//
}
// ..
Result *result= instance.callMe(MyClass::defaultState);
I am getting this:
argument of type "void (MyClass::)()" is incompatible with parameter of type "void ()()"
How to cast this correctly?
You can't cast it correctly, because pointers to member functions are different from pointers to regular functions. For starters, you must write &MyClass::defaultState, and the parameter has to be of type void(*MyClass::functionPointer)()
You can't even store &MyClass::defaultState directly in a std::function<void()>. What object would you call it on? But you could bind an instance, and store the bound result in a std::function<void()>.
The question was tagged "C", but C doesn't understand classes and can't call member functions.
Related
This question already has answers here:
Can a function return a pointer to its own type?
(2 answers)
Function Returning Itself
(10 answers)
Closed 3 months ago.
In this code a function returns a pointer to itself
typedef void (*voidfunc)();
voidfunc f(int) {
return (voidfunc) f;
}
and generates the assembly was expecting, but uses a cast to a different function type to do so. How can this be done without casting in C++?
In other words what can be placed instead of ??? in the following snippet?
??? f(int) {
return f;
}
I tried first with
auto f(int) {
return f;
}
but it doesn't work, then I tried with templates but it didn't work either.
It is impossible since the type of the function would need to be contain an infinite recursion.
The version with the cast is also not useful. Any actual use of the return value would need to cast back to the actual function type.
Without additional information about the use case it is difficult to make a recommendation to solve this.
This question already has answers here:
Why does argument matching in C++ ignore array sizes?
(1 answer)
In a function declaration, what does passing a fixed size array signify? [duplicate]
(3 answers)
Why can one specify the size of an array in a function parameter?
(3 answers)
Closed 3 years ago.
In C++, arrays are supposed to be passed to functions by reference. Hence, in what follows, function foo should implicitly use array inp by reference.
void foo(double inp[10]) {}
void foo1(double (&inp)[10]) {}
My question is, since both functions supposedly have the same interpretation of the input variable, why can we call foo in what follows, but we cannot call foo1?
int main()
{
double ary[20];
foo(ary); // compiles without any problem.
foo1(ary); // compiler error: invalid initialization of reference of type ‘double (&)[10]’ from expression of type ‘double [20]’
return 0;
}
since both functions supposedly have the same interpretation of the input variable
But they don't. A function argument of the type double inp[10] is automatically adjusted to a pointer double*. The 10 plays no part in providing type information here. Since all arrays decay to pointers, that will allow you to pass an array of any size.
A reference to an array does not get adjusted however. The type information is still there, and it must be a reference to an array of exactly ten doubles. The only way to pass a reference to an array of any size is to have a separate function for it, which you may write a function template to accomplish
template<std::size_t N>
void foo2(double (&inp)[N]) {}
This question already has answers here:
Passing references to pointers in C++
(10 answers)
Closed 4 years ago.
I have two function signatures in C++
void printArray(int* arrayPtr);
void printArray(int*& arrayPtr);
I understand the 1st function. It says the function takes in a arrayPtr argument which is of type that's a pointer pointing to an integer.
Both function signature works, but I have a hard time understanding the 2nd signature(*&) and what benefits it offers?
It's exactly the same as type versus type&; the first is a value and the second is a reference. The fact that type is a pointer doesn't change that.
This question already has answers here:
Calling a function through its address in memory in c / c++
(6 answers)
Closed 5 years ago.
I have a function at a known memory address(for example: 0x11111111). The function returns an int, and takes a uint32_t pointer as its only argument.
How would I call this function using c++? I have seen a few examples of calling a function by its address, but I can't seem to find one that takes a pointer as its argument
EDIT:I seen that. Doesn't address how to call the function that takes a pointer as an argument
If you’re sure that there’s a function there, you could call it by casting the address to a function pointer of the appropriate type, then calling it. Here’s C code to do this:
typedef int (*FunctionType)(uint32_t*);
FunctionType function = (FunctionType)0x11111111;
function(arg);
This can easily be modified to support any number of function arguments and any return type you’d like. Just tweak the argument types list of the FunctionType typedef.
Or, in one line (gulp):
(((int (*)(uint32_t *)) 0x11111111)(arg);
This question already has answers here:
When to use references vs. pointers
(17 answers)
difference between a pointer and reference parameter?
(8 answers)
Closed 7 years ago.
I would like to know the difference between defining a function to take an argument to a reference of an object (case 1) and defining a function to take an argument to a pointer of an object (case 2).
Of course the way that you refer to the object itself differs, therefore I am asking about the different capabilities that one has by using the one or the other way when defining a function, as well as the context that the one or the other way of defining a function is used.
1) Reference to the object in the function's arguments
void function(T& t) {
// Procedure that might change
// the value of the object t.
// t is the object
}
void main() {
T object;
function(object);
}
2) Pointer to the object in the function's arguments
void function(T* t) {
// procedure that might change
// the value of the object *t
// *t is the object
}
void main() {
T object;
function(&object);
}
Note: there are some questions that discuss the difference between a pointer variable and a pointer variable, but this one examines the context of using the as function arguments.