member-function-pointers

Unresolved external symbol, with home-made delegate

心不动则不痛 提交于 2019-12-24 10:19:37
问题 I have a function Foo which takes a 2-parameter function as a parameter: void Foo(void (*fcn)(int, int*)); However, the type of function which I want to pass in ( func ) only takes 1 parameter * . typedef void (__stdcall *FuncCallBack)(int a); void Caller(FuncCallBack func) { Foo(????); } In C#, I would do something like: Foo((a,b) -> func(a)); I'm trying to do something similar with a delegate class (having worked out that I can't have a pointer to a bound member function, I've switched to

mem_fn to function of member object

妖精的绣舞 提交于 2019-12-24 04:08:09
问题 I was tinkering around with the std::mem_fn and couldn't manage to bind it to data/functions of an member of a struct (one layer deeper). I hope that the code shows the problem better than I can describe it, because I'm not familiar with the terminology. #include <functional> struct Int { Int(int _x = 0) : x(_x) {} int GetInt() const { return x; } int x; }; struct IntWrapper { IntWrapper(int _x = 0) : test(_x) {} int GetWrappedInt() const { return test.GetInt(); } Int test; }; int main() {

Pass pointer to member function compiles in MinGW-w64 but not in gcc

帅比萌擦擦* 提交于 2019-12-24 00:05:54
问题 I have a Worker object with a run() non static member function. An object has been created: Worker * worker = new Worker(); Doing: std::thread(Worker::run, worker); Compiles (an works) under MinGW-w64 (gcc 4.9.1) but under linux (gcc 5.2.1) from Ubuntu, I get the compilation error: Invalid use of non-static member function The code is compiled with -std=gnu++11 I understand that in the MinGW case, the pointer to the member function has a signature with a kind of Worker * this parameter,

How to store arbitrary method pointers in c++11?

为君一笑 提交于 2019-12-23 15:02:22
问题 I need a way to store a list of method pointers, but I don't care about what class they belong to. I had this in mind: struct MethodPointer { void *object; void (*method)(void); }; Then I could have have a function which takes an arbitrary method: template <typename T> void register_method(void(T::*method)(void), T* obj) { MethodPointer pointer = {obj, method); } void use_method_pointer() { ... MethodPointer mp = ... // call the method (mp.object->*method)(); ... } This obviously doesn't

passing member functions as parameters / c++

会有一股神秘感。 提交于 2019-12-23 13:53:48
问题 I would like to implement, in c++, a class b where it would be possible to do some kind of iteration through a member set encapsulating the type of that iterator. Like: b_object.for_each_x_do(function_f); so function_f would get everyone of the x members and do anything. Let's say: void function_f(x_member_type x){ cout << x << endl; } Ok. So I'm trying to achieve that through a code like: class b{ int *x; public: void foreach_x_do(void (*f)(int)){ while(*x++) // or any kind of iteration

Understanding error with taking address of non-static member to form pointer to member function?

江枫思渺然 提交于 2019-12-23 05:31:12
问题 I have an issue that I'm not sure how to get around. I am using a library whose instances of the class are constructed by being passed references to a few functions. void func_on_enter() {...} void func() {...} void func_on_exit(){...} State state_a(&func_on_enter, &func, &func_on_exit); I am writing a class that contains an instance of this class, and as such have attempted something like this: class MyClass{ private: void func_on_enter() {...} void func() {...} void func_on_exit(){...}

How Can I Pass an Undefined Method Like an Undefined Function

≡放荡痞女 提交于 2019-12-23 02:04:07
问题 Given that I was passing the undefined function: void foo(char, short); I learned how to obtain the type tuple of the arguments by calling decltype(m(foo)) with this function: template <typename Ret, typename... Args> tuple<Args...> m(Ret(Args...)); I would now like to pass an undefined method: struct bar { void foo(char, short); }; I had tried rewriting m like: template <typename Ret, typename C, typename... Args> tuple<Args...> m(Ret(C::*)(Args...)); But when I try to call this similarly

How to store templated objects in an STL container and member function call

╄→尐↘猪︶ㄣ 提交于 2019-12-22 09:45:42
问题 Suppose you have a class like template<class T> struct A { void foo() { // Need access to "T" here typedef typename someTrait<T>::someType T2; } }; and you would like to "register" (or store) instances of the class (or a pointers to it) with a container (probably STL) for later calling the foo() method of all registered instances. Since instances instantiated with different template parameters are to be stored ( A<int> , A<float> , ...) obviously one can't use a std::vector and store the

Function member pointer with private base

北战南征 提交于 2019-12-22 07:04:10
问题 The following code yields a compile time error: ' base::print ' : cannot access private member declared in class ' base_der ' However, I have made the member public in the derived class. Why doesn't this work? #include <iostream> using namespace std; class base { public: int i; void print(int i) { printf("base i\n"); } }; class base_der : private base { public: using base::print; }; int main() { // This works: base_der cls; cls.print(10); // This doesn't: void (base_der::* print)(int); print

Pointer to function to member function

萝らか妹 提交于 2019-12-21 20:27:01
问题 I want to use a library (nlopt) that has a function set_min_objective which takes a pointer to a numerical function myfunc and find its minimum. I would like to create a class that will contain a suitably initialized member function. set_min_objective would then find an optimum in a specific instance (myP in the example below). The call sequence is: opt.set_min_objective(myfunc, NULL); and I would like to use something like: opt.set_min_objective(myP.f, NULL); the error I get when compiling