member-function-pointers

C++ inheritance and member function pointers

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 writ...

How to typedef a pointer to method which returns a pointer the method?

Basically I have the following class: class StateMachine { ... StateMethod stateA(); StateMethod stateB(); ... }; The methods stateA() and stateB() should be able return pointers to stateA() and stateB(). How to typedef the StateMethod? ...

Pointers to virtual member functions. How does it work?

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 membe...

How to hash and compare a pointer-to-member-function ?

How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function? Example: I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function. How can i do that? Also how can i compare (std::less) those member funct...

How to write a generic "getData" function?

Hi, everyone! I have a class, say, "CDownloader", that reads some XML data and provides access by node names. It features some getter functions, something like this: BOOL CDownloader::getInteger ( const CString &name, int *Value ); BOOL CDownloader::getImage ( const CString &name, BOOL NeedCache, CImage *Image ); BOOL CDownloader::ge...

C++ Member Function Pointers and STL Algorithm Problem

Okay I have a bit of a problem which i'm struggling to solve. Basically I have an abstract functor class that overloads operator() and derived objects that implement it. I have a function (part of another class) that tries to take an Array of these functor classes and tries to pass a pointer to a member function to the std algorithm fo...

Is reinterpreting a member function pointer a 'good idea' ?

I have a worker thread, which holds a list of 'Thread Actions', and works through them as an when. template <class T> class ThreadAction { public: typedef void (T::*action)(); ThreadAction(T* t, action f) : func(f),obj(t) {} void operator()() { (obj->*func)(); } void (T::*func)(); T* obj; }; It's normally called like...

C++ Comparing Member Function Pointers

In C++, is it possible to define a sort order for pointers to member functions? It seems that the operator< is undefined. Also, it's illegal to cast to void*. class A { public: void Test1(){} void Test2(){} }; int main() { void (A::* const one)() = &A::Test1; void (A::* const two)() = &A::Test2; boo...

Function pointers to member functions in C++

I need to call a method that expects a function pointer, but what I really want to pass to it is a functor. Here's an example of what I'm trying to do: #include <iostream> #include "boost/function.hpp" typedef int (*myAdder)(int); int adderFunction(int y) { return(2 + y); } class adderClass { public: adderClass(int x) : _x(x) ...

specialization on const member function pointers

I am trying to specialize some utility code on const member functions, but have problems to get a simple test-case to work. To simplify the work i am utilizing Boost.FunctionTypes and its components<FunctionType> template - a MPL sequence which should contain the tag const_qualified for const member functions. But using the test-code be...

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

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::OnEv...

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

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 { ...

How to define a general member function pointer

Hello, I have created a Timer class that must call a callback method when the timer has expired. Currently I have it working with normal function pointers (they are declared as void (*)(void), when the Elapsed event happens the function pointer is called. Is possible to do the same thing with a member function that has also the signat...

How to get address of member function for local class defined in function (C++)

I am trying to do the following: Obtain the address of a member function from a class that was locally defined within a function. class ConnectionBase { }; template class<EventType, SinkType> class ConnectionImpl : public ConnectionBase { public: typedef void (SinkType::*EventCallback)(EventType const&); }; template<class EventType> ...

templated member function to boost multi index container

Hi, I have a boost multi index container thus. using namespace boost::multi_index; template < typename O > class Container { public: multi_index_container< O, indexed_by< ordered_unique< const_mem_fun< O, std::string, &O::name > > > > _container; }; As you can se...

Member Function Pointer with base class argument accepting derived class argument

So I'm working on this event management class. I'm storing a list of pointers to member functions of the signature void (Event*) where Event is just a struct that stores some random data at the moment. typedef boost::function<void(Event*)> Callback; typedef vector<Callback> CallbackList; class EventManager { public: template<typenam...

Calling a member function from a member function templated argument

Given the following code which I can't get to compile. template < typename OT, typename KT, KT (OT::* KM)() const > class X { public: KT mfn( const OT & obj ) { return obj.*(KM)(); // Error here. } }; class O { public: int func() const { ...

Change the address of a member function in C++

Hello, in C++, I can easily create a function pointer by taking the address of a member function. However, is it possible to change the address of that local function? I.e. say I have funcA() and funcB() in the same class, defined differently. I'm looking to change the address of funcA() to that of funcB(), such that at run time callin...

Passing member function pointer to member object in c++

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...

How to call a pointer to method from another method

I had this problem some time ago and I gave up but lately it returned. #include <iostream> class element2D; class node2D { public: void (element2D::*FunctionPtr)(); void otherMethod() { std::cout << "hello" << std::endl; ((this)->*(this->FunctionPtr))(); //ERROR<------------------- } }; class element2D { publ...