I want to have a member that is a pointer to member function. Then I can set this pointer to point at one of the other member functions and use it to call the function I really want. Essentially I have different ways to implement a function and I want to set a pointer to call the appropriate one. Also the class is a template class.
I can't find a way call the function via the function pointer.
For example:
template <typename T> class C
{
public:
typedef void(C<T>::*Cfunc)(int);
Cfunc cf;
void p1(int i) {
}
C (int i)
{
cf = &C<T>::p1;
}
};
int main ()
{
C<int> Try1(1);
(Try1.*C<int>::cf)(10);
return 0;
}
I get the error:
tc.cpp: In function ‘int main()’:
tc.cpp:5:11: error: invalid use of non-static data member ‘C<int>::cf’
Cfunc cf;
^
tc.cpp:16:16: error: from this location
(Try1.*C<int>::cf)(10);
Your pointer to member function is not a static variable and therefore you need an instance of C to access it
int main()
{
C<int> Try1(1);
(Try1.*Try1.cf)(10);
return 0;
}
Related
I am trying to wrap my head around passing method as function argument. Here is a simplified example which returns a compilation error that I don't understand
class B
{
private:
int j;
public:
void foo(int i){std::cout << i + this->j << std::endl;}
void setj(int J){j=J;}
};
class A
{
private:
B b;
public:
void call(void (B::*fun)(int i), int i) { b.*fun(i); }
void setBj(int j){b.setj(j);}
};
int main()
{
A a;
a.setBj(40);
a.call(B::foo, 2);
}
When compiled with
g++ -std=c++11 b.cpp -o b
I get
b.cpp:22:50: error: called object type 'void (B::*)(int)' is not a function or
function pointer
void call(void (B::*fun)(int i), int i) { b.*fun(i); }
~~~^
b.cpp:31:12: error: call to non-static member function without an object
argument
a.call(B::foo, 2);
~~~^~~
2 errors generated.
I don't understand the first error message. I understand that I am calling foo as if it was a static method, which it is not but I don't understand how to pass a non-static method.
Two problems.
To invoke a pointer to a member function, you need to first apply a pointer to member access operator, that obtains a callable expression. Then you add a call. Now it just so happens that .* is of lower precedence than the function call operator. So the first fix:
(b.*fun)(i)
A a pointer to member function can only be obtained by applying unary & on the fully qualified function name. So the second fix:
a.call(&B::foo, 2);
Hi it is my first experience with passing function pointer in C++.
So here is my code:-
#include <iostream>
using namespace std;
// Two simple functions
class student
{
public:
void fun1() { printf("Fun1\n"); }
void fun2() { printf("Fun2\n"); }
// A function that receives a simple function
// as parameter and calls the function
void wrapper(void (*fun)())
{
fun();
}
};
int main()
{ student s;
s.wrapper(s.fun1());
s.wrapper(s.fun2());
return 0;
}
Initially in wrapper function i passed only fun1 and fun2.I got an error
try.cpp:22:15: error: ‘fun1’ was not declared in this scope
s.wrapper(fun1);
^~~~
try.cpp:23:15: error: ‘fun2’ was not declared in this scope
s.wrapper(fun2);
Later I tried to pass s.fun1() and s.fun2() as argument but again got error
try.cpp:23:23: error: invalid use of void expression
s.wrapper(s.fun1());
^
try.cpp:24:23: error: invalid use of void expression
s.wrapper(s.fun2());
Please help I don't know what to do :(
Let's deal with the two issues in the post.
You are calling fun1 and fun2. Since their return type is void, you can't pass their result as something's value. In particular as the value of a function pointer. You also can't obtain their address by using the dot member access operator. Which brings us to the following.
Member functions are not like regular functions. You cannot just take their address. Their treatment is special, because member functions can only be called on an object. So there's a special syntax for them, which involves the class they belong to.
Here's how you would do something like what you are after:
class student
{
public:
void fun1() { printf("Fun1\n"); }
void fun2() { printf("Fun2\n"); }
// A function that receives a member function
// as parameter and calls the function
void wrapper(void (student::*fun)())
{
(this->*fun)();
}
};
int main()
{ student s;
s.wrapper(&student::fun1);
s.wrapper(&student::fun2);
return 0;
}
I'm having trouble understanding function signatures and pointers.
struct myStruct
{
static void staticFunc(){};
void nonstaticFunc(){};
};
int main()
{
void (*p)(); // Pointer to function with signature void();
p = &myStruct::staticFunc; // Works fine
p = &myStruct::nonstaticFunc; // Type mismatch
}
My compiler says that the type of myStruct::nonstaticFunc() is void (myStruct::*)(), but isn't that the type of a pointer pointing to it?
I'm asking because when you create an std::function object you pass the function signature of the function you want it to point to, like:
std::function<void()> funcPtr; // Pointer to function with signature void()
not
std::function<void(*)()> funcPtr;
If I had to guess based on the pattern of void() I would say:
void myStruct::();
or
void (myStruct::)();
But this isn't right. I don't see why I should add an asterisk just because it's nonstatic as opposed to static. In other words, pointer void(* )() points to function with signature void(), and pointer void(myStruct::*)() points to function with signature what?
To me there seems to be a basic misunderstanding of what a member pointer is. For example if you have:
struct P2d {
double x, y;
};
the member pointer double P2d::*mp = &P2d::x; cannot point to the x coordinate of a specific P2d instance, it is instead a "pointer" to the name x: to get the double you will need to provide the P2d instance you're looking for... for example:
P2d p{10, 20};
printf("%.18g\n", p.*mp); // prints 10
The same applies to member functions... for example:
struct P2d {
double x, y;
double len() const {
return sqrt(x*x + y*y);
}
};
double (P2d::*f)() const = &P2d::len;
where f is not a pointer to a member function of a specific instance and it needs a this to be called with
printf("%.18g\n", (p.*f)());
f in other words is simply a "selector" of which of the const member functions of class P2d accepting no parameters and returning a double you are interested in. In this specific case (since there is only one member function compatible) such a selector could be stored using zero bits (the only possible value you can set that pointer to is &P2d::len).
Please don't feel ashamed for not understanding member pointers at first. They're indeed sort of "strange" and not many C++ programmers understand them.
To be honest they're also not really that useful: what is needed most often is instead a pointer to a method of a specific instance.
C++11 provides that with std::function wrapper and lambdas:
std::function<double()> g = [&](){ return p.len(); };
printf("%.18g\n", g()); // calls .len() on instance p
std::function<void()> funcPtr = std::bind(&myStruct::nonstaticFunc, obj);
Is how you store a member function in std::function. The member function must be called on a valid object.
If you want to delay the passing of an object until later, you can accomplish it like this:
#include <functional>
#include <iostream>
struct A {
void foo() { std::cout << "A::foo\n"; }
};
int main() {
using namespace std::placeholders;
std::function<void(A&)> f = std::bind(&A::foo, _1);
A a;
f(a);
return 0;
}
std::bind will take care of the details for you. std::function still must have the signature of a regular function as it's type parameter. But it can mask a member, if the object is made to appear as a parameter to the function.
Addenum:
For assigning into std::function, you don't even need std::bind for late binding of the object, so long as the prototype is correct:
std::function<void(A&)> f = &A::foo;
p = &myStruct::staticFunc; // Works fine
p = &myStruct::nonstaticFunc; // Type mismatch
Reason : A function-to-pointer conversion never applies to non-static member functions because an lvalue that refers to a non-static member function
cannot be obtained.
pointer void(* )() points to function with signature void(), and pointer void(myStruct::*)() points to function with signature what?
myStruct:: is to make sure that the non-static member function of struct myStruct is called (not of other structs, as shown below) :
struct myStruct
{
static void staticFunc(){};
void nonstaticFunc(){};
};
struct myStruct2
{
static void staticFunc(){};
void nonstaticFunc(){};
};
int main()
{
void (*p)(); // Pointer to function with signature void();
void (myStruct::*f)();
p = &myStruct::staticFunc; // Works fine
p = &myStruct2::staticFunc; // Works fine
f = &myStruct::nonstaticFunc; // Works fine
//f = &myStruct2::nonstaticFunc; // Error. Cannot convert 'void (myStruct2::*)()' to 'void (myStruct::*)()' in assignment
return 0;
}
When you use a pointer, std::function or std::bind to refer to a non-static member function (namely, "method" of class Foo), the first param must be a concrete object of class Foo, because non-static method must be called by a concrete object, not by Class.
More details: std::function and
std::bind.
The answer is in the doc.
Pointer to member declarator: the declaration S C::* D; declares D as
a pointer to non-static member of C of type determined by
decl-specifier-seq S.
struct C
{
void f(int n) { std::cout << n << '\n'; }
};
int main()
{
void (C::* p)(int) = &C::f; // pointer to member function f of class C
C c;
(c.*p)(1); // prints 1
C* cp = &c;
(cp->*p)(2); // prints 2
}
There are no function with signature void (). There are void (*)() for a function or void (foo::*)() for a method of foo. The asterisk is mandatory because it's a pointer to x. std::function has nothing to do with that.
Note: Your confusion is that void() is that same signature that void (*)(). Or even int() <=> int (*)(). Maybe you think that you can write int (foo::*) to have a method pointer. But this is a data member pointer because the parenthesis are optional, int (foo::*) <=> int foo::*.
To avoid such obscure syntax you need to write your pointer to function/member with the return type, the asterisk and his parameters.
class A {
int f(int x, int j) { return 2;}
decltype(f)* p;
};
Gives me the error:
error: decltype cannot resolve address of overloaded function
I can't understand why that error is even speaking of overloaded functions. Similarly I thought that maybe I needed to use the scope operator to access the function:
class A {
int f(int x, int j) { return 2;}
decltype(A::f)* p;
};
Which still gives me an error but a clearer description:
error: invalid use of non-static member function 'int A::f(int, int)'
Why is it that I'm not allowed to use decltype to find the type of a member function? Alternatively setting the member function to static removes the error in either case.
What you really want is:
struct a {
int f(int x, int j) { return 2;}
decltype(&a::f) p;
};
Live demo
Since the f you are referring to is a member function. The deduced type is:
int(a::*)(int, int)
Without the & the compiler is assuming that you are trying to call the function without providing arguments to it. Perhaps Clang's error message is clearer about this:
error: call to non-static member function without an object argument
decltype(a::f) p;
If you really don't want the pointer type you can later apply std::remove_pointer_t from <type_traits>.
typedef void (*callable_function)(double);
void call_function(callable_function func)
{
func(3.0);
}
class Foo;
union converter {
callable_function func;
void (Foo::*foo_func)(void);
};
class Foo {
private:
double d;
public:
Foo(void) : d(0.0)
{
converter c;
c.foo_func = &Foo::set_double;
call_function(c.func);//I know i can call the function directly, but that is not what i want to achieve
}
void set_double(double value)
{
d = value;
}
};
void main(void)
{
Foo foo;
}
When trying to execute the code above, i get a heap corruption error in the line:
d = value;.
Probably because the function being called is the class function, not the member function.
But if i try to change this line: c.foo_func = &Foo::set_double; to c.foo_func = &this->set_double;, i get this compiler error:
error C2276: '&' : illegal operation on bound member function expression
Question #1: Is there any way to convert the pointer(casting maybe) from void(Foo::*)(void) to void()(void) or to fix the compiler error?
pointer to "free" function and pointer to member function in C++ have different sizes.
That means you technically can cast pointer to free function to void* ptr as usually they have the same size but you cannot cast member function pointer to void* or other free function - you will get what you've got.
Check this: Pointers to members representations