Passing implicit function pointers [duplicate] - c++

This question already has answers here:
About Pointers To Functions in function declarations
(4 answers)
Closed 7 years ago.
In some C++ 98 code (meaning no, using std::function is not an option), I found the following construct:
class someClass
{
public:
typedef void callback();
void setCallback(callback c)
{
mCallback = c;
}
void callCallback()
{
if (mCallback)
mCallback();
}
private:
callback *mCallback;
};
This confused me. I am used to passing callback functions as a function pointer, so I would expect setCallback to take (*callback)() as argument.
However, the above code seems to work, and compiles without any (related) warnings.
Could someone tell me what is happening here? Is my callback function implicitly passed as a function pointer? Is it a good idea to use this instead of function pointers?
The only thing I could find is that this construction results in "parameter-declaration-clause" ambiguity (C++ 98 8.3p7). Is this the only downside? Are there any benefits?

Similarly to arrays, parameters of function type declare, in fact, a pointer to that type.

Related

Passing a member function to a base class constructor results in "invalid use of non-static function..." [duplicate]

This question already has answers here:
How can I pass a member function where a free function is expected?
(9 answers)
how to pass a non static-member function as a callback?
(8 answers)
Pass Member Function as Parameter to other Member Function (C++ 11 <function>) [duplicate]
(2 answers)
Closed 4 months ago.
I need to change the prototype of a function pointer of a class. So, I was hoping inheriting it and doing the following would work, but it doesn't ("invalid use of non-static member function 'void B::myIntCallback(unsigned int)"):
class A {
public:
typedef void (*intCallback_t)(unsigned int myInt);
A(intCallback_t intCallback) {}
};
class B : A {
public:
typedef void (*charCallback_t)(unsigned char myChar);
B(charCallback_t charCallback) : A(this->myIntCallback) {
charCallback_ = charCallback;
}
private:
charCallback_t charCallback_;
void myIntCallback(unsigned int myInt) {
charCallback_((unsigned char)myInt);
}
};
Does anybody know how I can solve this? I can't change class A.
You are trying to pass a member function (B::myIntCallback) to a function pointer argument. Since the member function needs an object to be called on, his would require some kind of capturing, e.g. a lambda capturing this or an std::bind expression. Unfortunately, neither is possible with a plain function pointer, see also Passing capturing lambda as function pointer.
If possible, you may want to consider changing the class A to accept either a std::function or a template argument as the type of the callback. See also Should I use std::function or a function pointer in C++?.

How to make a callback in C++: use a class member function as a parameter [duplicate]

This question already has answers here:
Callback functions in C++
(11 answers)
Closed 2 years ago.
I started using C++ recently and at one point I needed to set up a callback, i.e. call a function of another class and pass to it as a parameter the name of one of my own functions so that it calls it back. At first I tried this:
void myOtherFunction(void (*oneOfMyFunctions)(void)) {
oneOfMyFunctions();
}
Unfortunately this code doesn't support class member functions because it is (correct me if I am wrong) ...C code.
This can work.
void myOtherFunction(void (*oneOfMyFunctions)(void)) {
oneOfMyFunctions();
}
However, your problem may be due to trying to pass member functions into this function. If member_function is a member function of class A, the expression &member_function inside class A has a type of void (A::*)(void), not void (*)(void) like you want (that is, it wants an A pointer in addition to its normal parameters). You can use std::bind():
std::bind(&member_function, this)
to create a function object which can be called with an empty parameter list. However, then you would need to change your member function signature to something like this:
template <typename FuncType>
void myOtherFunction(FuncType oneOfMyFunctions) {
oneOfMyFunctions();
}
or, like Th0rgal may have said,
void myOtherFunction(std::function<void()> oneOfMyFunctions) {
oneOfMyFunctions();
}
Here is a working way to do that:
void myOtherFunction(std::function<void()> oneOfMyFunctions) {
oneOfMyFunctions();
}
And inside my class:
myOtherFunction([&] {
oneOfMyFunctions();
});
Some explanations:
In std::function<void()>, void is what is returned by the function and () contains the types of its parameters (mine is empty because it doesn't have any).
In the 2nd code I am using a lambda to keep the context, as a bind would do (but lambdas replace them).

unexpected compilation error when passing by reference anonymous instance [duplicate]

This question already has answers here:
How come a non-const reference cannot bind to a temporary object?
(11 answers)
Closed 7 years ago.
Let's say I have a struct :
struct structazauras
{
string bla;
}
And some function (in my case this function is actually a constructor of some class but I don't think this is the issue):
void myfunc(structazauras& mystruct) { }
then some where i call myfunc :
..
myfunc(structazauras());
..
I get an error:
no matching function for call to myfunc(structazauras) candidates are myfunc(structazauras&)
If I change the code to :
structazauras tmp;
myfunc(tmp);
It will work fine.
I feel that he (the compiler) has a problem passing a reference to the anonymous instance if structazauras, but why ? the anonymous instance exist on the stack of the calling function.
That is because you cannot bind a temporary to a non-const reference. Mark your reference as const and it will work.
Also, you are using a standard C++ keyword (struct) in the definition
void myfunc(structazauras& struct) { }
Change the name to something else. Or maybe you meant
void myfunc(struct structazauras&) { }
but the additional struct is superfluous in C++.

C++ Struct internal declaration confusion? [duplicate]

This question already has an answer here:
Factory Pattern: typedef Class *(createClassFunction)(void)
(1 answer)
Closed 8 years ago.
I've come across a declaration inside a C++ Struct{..} that I've never seen before.
Can anyone tell me what it means;
struct DerivedMesh {
char cd_flag;
void (*calcNormals)(DerivedMesh *dm); // <-- What is this?
It kind of looks like it's dereferencing a pointer called calcNormals, but that's all I can make out.
This is a C syntax for declaring function pointers.
In this particular example, DerivedMesh will have a member calcNormals that is a pointer to a function accepting single argument of type DerivedMesh*. It can be called like an ordinary function:
void foo(DerivedMesh* dm) { ... }
DerivedMesh dm;;
// Init members and set calcNormals to actual function
dm.cf_flag = whatever;
dm.calcNormals = foo;
dm.calcNormals(&dm); // calls foo
This
void (*calcNormals)(DerivedMesh *dm);
is class data member definition with name calcNormals that has type of pointer to function of type void( DerivedMesh * )

callback function syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does void(U::*)(void) mean?
Considering the following:
template <class T>
class myButtoncb {
private:
T *ptr;
void (T::*cback) (void)
}
What I understand is:
void (*cback) (void)
Which is nothing but a function pointer that to a function that returns void, and takes no argument.
What I dont understand is, what is the importance of T::? Isn't it enough to declare
only like void (*cback) (void) ?
This says that it's a member function that has a this pointer. Otherwise, it would be a free function, wouldn't have any idea what object it was operating on, and wouldn't be able to access any non-static member functions or member variables.
From C++ FAQ
Is the type of "pointer-to-member-function" different from "pointer-to-function"?
Yep.
Link which I've provided to you has a lot of information about this topic.
The function, you pass there, must be declared inside the class T - the template parameter of myButtoncb. So you can use a function like the following:
class A
{
public:
void foo(void);
};
myButton<A> b;
b.cback = &A::foo;