468,792 Members | 1,779 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,792 developers. It's quick & easy.

map value as function pointer

Hi, I want to know How to create an associated array using map with
keys are string, values are function pointers with same arguments, but
each function belong to different class. The map need to be initialized
static, what's the syntax to retrieve a function pointer?

May 8 '06 #1
9 5685
Allen wrote:
Hi, I want to know How to create an associated array using map with
keys are string, values are function pointers with same arguments, but
each function belong to different class. The map need to be initialized
static, what's the syntax to retrieve a function pointer?


You can only do this if the functions are non-members or static
members. For the syntax, see these FAQs:

http://www.parashift.com/c++-faq-lit...o-members.html

Cheers! --M

May 8 '06 #2
class ITerminalServerListener;
class CTerminalServer;
typedef VOID (CTerminalServer::*TrapFunc)(vector<Vb>
&l_VbList,ITerminalServerListener *l_EventListener);

class CTerminalServer {
map<OID,TrapFunc,OIDCMP> m_FunctionMap
};
works for me with gcc 2.96

and none of TrapFunc functions are static
will it not work for later versions of the C++ compiler ?

i call functions extracted from the map using this

(this->*l_TrapProcessFunction)(a,b);

-SIGTERM
amit

May 10 '06 #3
Amit Limaye wrote:
class ITerminalServerListener;
class CTerminalServer;
typedef VOID (CTerminalServer::*TrapFunc)(vector<Vb>
&l_VbList,ITerminalServerListener *l_EventListener);

class CTerminalServer {
map<OID,TrapFunc,OIDCMP> m_FunctionMap
};
works for me with gcc 2.96

and none of TrapFunc functions are static
will it not work for later versions of the C++ compiler ?

i call functions extracted from the map using this

(this->*l_TrapProcessFunction)(a,b);

-SIGTERM
amit


But the OP said that "each function belong to different class." Yours
all belong to the same class, viz. CTerminalServer.

Cheers! --M

May 10 '06 #4
my mistake sorry

-SIGTERM
amit

May 10 '06 #5

mlimber wrote:
Allen wrote:
Hi, I want to know How to create an associated array using map with
keys are string, values are function pointers with same arguments, but
each function belong to different class. The map need to be initialized
static, what's the syntax to retrieve a function pointer?


You can only do this if the functions are non-members or static
members. For the syntax, see these FAQs:

http://www.parashift.com/c++-faq-lit...o-members.html

Cheers! --M


But function objects (e.g. from boost) will let you get around this.

May 10 '06 #6
An**********@gmail.com wrote:
mlimber wrote:
Allen wrote:
Hi, I want to know How to create an associated array using map with
keys are string, values are function pointers with same arguments, but
each function belong to different class. The map need to be initialized
static, what's the syntax to retrieve a function pointer?


You can only do this if the functions are non-members or static
members. For the syntax, see these FAQs:

http://www.parashift.com/c++-faq-lit...o-members.html

Cheers! --M


But function objects (e.g. from boost) will let you get around this.


That's true *if* you can supply the object instances when building the
map. See Boost's bind and mem_fn libraries.

Cheers! --M

May 10 '06 #7

mlimber wrote:
An**********@gmail.com wrote:

But function objects (e.g. from boost) will let you get around this.


That's true *if* you can supply the object instances when building the
map. See Boost's bind and mem_fn libraries.

Cheers! --M


Well before we look into better ways of doing it, the simplest way for
"free" functions.

typedef your function. Let's say your function takes a const char *
parameter and returns int.

typedef int ( *func_type ) ( const char * );

and now you can have std::map< std::string, func_type >

The alternative using a functor would be to use a single functor type
that takes a function pointer as its constructor. The advantage is only
one of clean syntax. Thus you can have a functor eg:

template< typename ReturnType, typename Param0, typename Param1 >
class function2
{
public:
typedef ReturnType ( * func_type ) ( Param0, Param1 );

private:
func_type func;

public:
function2( func_type f ) : func( f ) {}

ReturnType operator() ( Param0 p0, Param1 p1 )
{
return func( p0, p1 );
}
};

Not sure that is totally accurate (do I need (*func)(p0,p1) ? )

boost functions are similar to what I have illustrated. I can't
remember how high they go.

The other option is to use an abstract base class. Now you don't define
different functions, you define classes. That is the way I prefer doing
it, by the way.

If you have existing free-functions you want to use, you can always
create instantiations of the abstract base class that use them (or even
that take one as a parameter and use it so you need write only one such
instantiation).

Now you can have global instances of these classes - they don't have
state so are re-entrant and they don't really use up any resource in
their construction. You can also write libraries that create new
instances, and even get them to add themselves to your map, thus
providing a system of "add-ins" to your application.

May 10 '06 #8
like this:

struct GEvent
{
virtual void invoke() = 0;
virtual ~GEvent(){
};
};

template <class T>
struct GInvokeHandler : public GEvent{
T* p;
void (T::*pmfn) ();
GInvokeHandler(T *_p, void ( T::*_pmfn) ()){
p = _p;
pmfn = _pmfn;
}
void invoke()
{
return (p->*pmfn) ();
}
};

long foo(T *p, void (T::*pmfn)()){
GEvent* pEvent = new GInvokeHandler<T>(p,pmfn);
map<long, GEvent*> _map;
...
}
Allen wrote:
Hi, I want to know How to create an associated array using map with
keys are string, values are function pointers with same arguments, but
each function belong to different class. The map need to be initialized
static, what's the syntax to retrieve a function pointer?


May 12 '06 #9
Please don't top-post.

Darwin Lalo wrote:
like this:

struct GEvent
{
virtual void invoke() = 0;
virtual ~GEvent(){
};
};

template <class T>
struct GInvokeHandler : public GEvent{
T* p;
void (T::*pmfn) ();
GInvokeHandler(T *_p, void ( T::*_pmfn) ()){
p = _p;
pmfn = _pmfn;
}
See http://www.parashift.com/c++-faq-lit....html#faq-10.6.
void invoke()
{
return (p->*pmfn) ();
}
};

long foo(T *p, void (T::*pmfn)()){
GEvent* pEvent = new GInvokeHandler<T>(p,pmfn);
map<long, GEvent*> _map;
...
}


This is basically the same as using a functor to call a member function
as mentioned previously on this thread, and, as also previously
mentioned, it would work fine *if* the user can supply the object
instance on which to invoke the member function when building the map.
If not, it must be done with static members or non-member functions.
(This disclaimer also assumes that the "different" classes mentioned in
the OP cannot be manipulated through static or dynamic polymorphism.)

Cheers! --M

May 16 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by Riccardo Rossi | last post: by
17 posts views Thread by Christopher Benson-Manica | last post: by
23 posts views Thread by Tomás | last post: by
40 posts views Thread by Zach | last post: by
14 posts views Thread by Abhi | last post: by
10 posts views Thread by Robert Dailey | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.