member-function-pointers

C++ inheritance and member function pointers

我的梦境 提交于 2019-11-27 19:15:28
In C++, can member function pointers be used to point to derived (or even base) class members? EDIT: Perhaps an example will help. Suppose we have a hierarchy of three classes X , Y , Z in order of inheritance. Y therefore has a base class X and a derived class Z . Now we can define a member function pointer p for class Y . This is written as: void (Y::*p)(); (For simplicity, I'll assume we're only interested in functions with the signature void f() ) This pointer p can now be used to point to member functions of class Y . This question (two questions, really) is then: Can p be used to point

C++: Pointer to monomorphic version of virtual member function?

99封情书 提交于 2019-11-27 19:03:09
In C++, it's possible to get a pointer to a (non-static) member function of a class, and then later invoke it on an object. If the function was virtual, the call is dispatched dynamically depending on the dynamic type of the object. It's also possible (not using a member pointer) to call virtual member functions of objects monomorphically, by explicitly providing the scope containing the version to use. The following code demonstrates this: #include <iostream> using std::cout; using std::endl; struct Foo { virtual void foo() { cout << 1 << endl; } }; struct Foo2: public Foo { virtual void foo(

What are the rules for function pointers and member function pointers to Standard functions?

狂风中的少年 提交于 2019-11-27 18:38:34
问题 What are the existing rules for taking function pointers or member function pointers to Standard functions? For example, something like auto p = &std::string::size; Is this legal? Would it be more or less legal if I explicitly requested the correct type, so it would function even if there was an additional implementation-added overload of std::string::size ? 回答1: Using the "correct" type doesn't make things better: Except for the virtual functions all functions in the standard C++ library can

Passing member function pointer to member object in c++

梦想与她 提交于 2019-11-27 18:35:17
I have a problem with using a pointer to function in C++. Here is my example: #include <iostream> using namespace std; class bar { public: void (*funcP)(); }; class foo { public: bar myBar; void hello(){cout << "hello" << endl;}; }; void byebye() { cout << "bye" << endl; } int main() { foo testFoo; testFoo.myBar.funcP = &byebye; //OK testFoo.myBar.funcP = &testFoo.hello; //ERROR return 0; } Compilator returns an error at testFoo.myBar.funcP = &testFoo.hello; : ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say '&foo::hello' cannot convert

Pointers to virtual member functions. How does it work?

梦想的初衷 提交于 2019-11-27 13:55:55
Consider the following C++ code: class A { public: virtual void f()=0; }; int main() { void (A::*f)()=&A::f; } If I'd have to guess, I'd say that &A::f in this context would mean "the address of A's implementation of f()", since there is no explicit seperation between pointers to regular member functions and virtual member functions. And since A doesn't implement f(), that would be a compile error. However, it isn't. And not only that. The following code: void (A::*f)()=&A::f; A *a=new B; // B is a subclass of A, which implements f() (a->*f)(); will actually call B::f. How does it happen? Here

boost::bind & boost::function pointers to overloaded or templated member functions

試著忘記壹切 提交于 2019-11-27 13:52:54
问题 I have a callback mechanism, the classes involved are: class App { void onEvent(const MyEvent& event); void onEvent(const MyOtherEvent& event); Connector connect; } class Connector { template <class T> void Subscribe(boost::function <void (const T&)> callback); } App::App() { connect.Subscribe<MyEvent>(&App::OnEvent<MyEvent>); } First off this code doesn't compile, it's an illustration. The use of templates complicates my example, but I left them in because its affecting my problem. It seems

How Can I Pass a Member Function to a Function Pointer?

我怕爱的太早我们不能终老 提交于 2019-11-27 08:57:00
class Child; class Parent { public: void (*funcPointer)(); void (*funcPointer2)(Parent* _this); void (Child::*funcPointer3)(); }; class Child: public Parent { public: void TestFunc(){ } void Do(){ Parent p; p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)' p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)' p.funcPointer3=TestFunc; //this works p.funcPointer3=&Child::TestFunc; // this works too. p.funcPointer3(); // error, term does not

How do I call a pointer-to-member-function?

允我心安 提交于 2019-11-27 08:44:18
I'm getting a compile error (MS VS 2008) that I just don't understand. After messing with it for many hours, it's all blurry and I feel like there's something very obvious (and very stupid) that I'm missing. Here's the essential code: typedef int (C::*PFN)(int); struct MAP_ENTRY { int id; PFN pfn; }; class C { ... int Dispatch(int, int); MAP_ENTRY *pMap; ... }; int C::Dispatch(int id, int val) { for (MAP_ENTRY *p = pMap; p->id != 0; ++p) { if (p->id == id) return p->pfn(val); // <--- error here } return 0; } The compiler claims at the arrow that the "term does not evaluate to a function taking

Invalid use of non-static member function c++

青春壹個敷衍的年華 提交于 2019-11-27 08:00:50
问题 I am following this example. But when I compile, it returns an error: Invalid use of non-static member function at the line void(Machine:: *ptrs[])() = { Machine::off, Machine::on }; I tried to add static to void on(); at class class Machine { class State *current; public: Machine(); void setCurrent(State *s) { current = s; } static void on(); // I add static here ... static void off(); // and here }; But it complains that Invalid use of member Machine::current in static member function Can

c++ member function pointer problem

守給你的承諾、 提交于 2019-11-27 06:52:37
问题 I'm new to c++ . I want to know about object pointer and pointer to member function . I wrote a code which is following: code : #include <iostream> using namespace std; class golu { int i; public: void man() { cout<<"\ntry to learn \n"; } }; int main() { golu m, *n; void golu:: *t =&golu::man(); //making pointer to member function n=&m;//confused is it object pointer n->*t(); } but when i compile it it shows me two error which is following: pcc.cpp: In function ‘int main()’: pcc.cpp:15: error