member-function-pointers

Member function pointer wrapper using variadic template

爱⌒轻易说出口 提交于 2019-12-11 10:35:44
问题 I'm currently trying to compile my code using Visual C++ 2013 and want to take the advantage of variadic templates. I have several classes which wrap the function pointer - several versions for different number of arguments. The wrapper for a member function with one argument is the following: template <typename T, T> struct proxy; template <typename T, typename R, typename Arg0, R(T::*mf)(Arg0)> struct proxy<R(T::*)(Arg0), mf> { proxy(T& host) : m_Host(host) {} template <typename Arg0> R

Call member function

孤者浪人 提交于 2019-12-11 08:32:26
问题 I don't know how to call a class member function from main. I want to call "printPoly" with a poly object as its implicit parameter. Here is the class definition: class poly { private: Node *start; public: poly(Node *head) /*constructor function*/ { start = head; } void printPoly(); //->Poly *polyObj would be the implicit parameter?? }; void poly :: printPoly() { //.... } Here is the calling code: poly *polyObj; polyObj = processPoly(polynomial); // processPoly is a function that returns a

How to pass a member function pointer to an overloaded method in a template function?

跟風遠走 提交于 2019-12-11 03:57:38
问题 I referred to this somewhat similar question. However here the scenario is different: struct A { void foo (int i) {} // choice void foo (double i) {} }; template<typename ObjType, typename FuncPtr> void ReceiveFuncPtr (ObjType o, FuncPtr pf) { (o.*pf)(1); } int main () { A obj; ReceiveFuncPtr(obj, &A::foo); // don't want typecast here } In the above test code, I have an overloaded foo inside A . Had there been only 1 foo then the code works fine. But for overloading case, compiler complains

How to invoke pointer to member function from static member function?

眉间皱痕 提交于 2019-12-10 17:20:17
问题 I need to get a member function called by a standard function pointer, so I tried to abstract things like this: class Sample { public: virtual void doSomething(void) = 0; }; class A : public Sample { void doSomething(void); // details omitted }; class B : public Sample { void doSomething(void); // details omitted }; class Executor { public: Executor(Sample *sample) : func(&sample->doSomething) { } static void *execute(void *data) { Executor *pX = data; (pX->*func)(); // error invalid access

C++11: pointers to member function using std::function::target()

烈酒焚心 提交于 2019-12-10 14:04:28
问题 I know this is already a long discussed topic, but I couldn't yet find an answer that satisfies me. Question in short: even using the C++11's function::target() capabilities, is it not possible to pass member function pointers to c-style methods? The following code will not work: the invocation to mbf.target() will return 0 and thus a SEGFAULT is produced. And I don't understand why, because I bind the member function to a generic function object, so the type should be fine. What am I doing

Pass member function pointer in C++

 ̄綄美尐妖づ 提交于 2019-12-10 11:24:11
问题 I am trying to pass a function pointer (of type QScriptEngine::FunctionSignature (= QScriptValue (QScriptContext *, QScriptEngine *) )) to an other function. But the function I need to pass is a member function of a class. I use it like this: class MyClass { SomeVarType someVarINeedAccessTo; QScriptValue print(QScriptContext* context, QScriptEngine* engine) { ... someVarINeedAccessTo ... } void someFunction() { QScriptEngine engine; QScriptValue printFunction = engine.newFunction(print);

Calling Member Function Pointers

帅比萌擦擦* 提交于 2019-12-10 10:12:54
问题 I am having trouble calling a function pointer inside a structure. I have used this approach before outside of classes, but now that I am trying it inside a class method using function pointers to other class methods.... I am receiving a compiler error. Here is my class: class Myclass { int i; void cmd1(int) {} void cmd2(int) {} void trans() { const struct { std::string cmd; void (Myclass::*func)(int) } CmdTable[] = { { "command1", &Myclass::cmd1 }, { "command2", &Myclass::cmd2 } }; CmdTable

Pointer to member function syntax

北慕城南 提交于 2019-12-10 09:46:09
问题 I'm trying to wrap my head around pointers to member functions and am stuck with this example: #include <iostream> class TestClass { public: void TestMethod(int, int) const; }; void TestClass::TestMethod(int x, int y) const { std::cout << x << " + " << y << " = " << x + y << std::endl; } int main() { void (TestClass::*func)(int, int) const; func = TestClass::TestMethod; TestClass tc; tc.func(10, 20); return 0; } What I think the code should do: In the first line of main I declare a pointer to

C++ - is it possible to extract class and argument types from a member function type in a template?

。_饼干妹妹 提交于 2019-12-09 10:53:14
问题 I would like to wrap member functions that conform to the type 'void (ClassType::Function)(ArgType)' with a templated class. Later, I want to pass an instance of ClassType to an instance of this template and have it invoke the wrapped method: class Foo { public: Foo() : f_(0.0) {} void set(double v) { f_ = v * 2.1; } double get() { return f_; } private: double f_; }; template <typename ArgType, typename ClassType, void (ClassType::*Method)(ArgType)> class Wrapper { public: explicit Wrapper

How to best pass methods into methods of the same class

微笑、不失礼 提交于 2019-12-08 23:53:45
问题 I have this C++ class that one big complicated method compute that I would like to feed with a "compute kernel", a method of the same class. I figure I would do something along the lines of class test { int classVar_ = 42; int compute_add(int a, int b) { compute(int a, int b, this->add_()) } int compute_mult(int a, int b) { compute(int a, int b, this->mult_()) } int compute_(int a, int b, "pass in add or multiply as f()") { int c=0; // Some complex loops { c += f(a,b) // } return c; } int add