Command pattern problem

A

alexcpn

Hi I have a Command class of type

class Command
{
T* m_objptr;
void (T::*method)();

:

explicit Command(T* pObj,void (T::*p_method)(),long timeout,const
char* key,long priority = PRIO_NORMAL ):
m_objptr(0),method(0),m_timeout(timeout),m_key(key),m_value(priority)
{
m_objptr=pObj;
method = p_method;
}

inline void execute()
{
(m_objptr->*method)();
}
//-----------------------------------------------------------------------

I use this as

cmd= new Command said:
GetTimeOut(),"ne2_11"PRIO_NORMAL);

where CTask is defined as

class CTask
{
void DoSomeTask()

Question 1
Is this usage safe? ie will &CTask::DoSomeTask point to the method
of CTask ?

I use this in a threadpool and I am sometimes getting thiings to work
and sometimes access violation (VC6 compiler)

Question 2
Is it possible that I can pass a parameter to the method

something like

class Command
{
T* m_objptr;
void (T::*method)(void *);


class CTask
{
void DoSomeTask(void *) or
static void DoSomeTask(void *)

If so how do you initiate Command class - I was getting compiler error
while doing this

cmd= new Command said:
GetTimeOut(),"ne2_11"PRIO_NORMAL);

Thanks for your time
Alex.C.P
 
M

Michael DOUBEZ

alexcpn a écrit :
Hi I have a Command class of type

class Command
{
T* m_objptr;
void (T::*method)();

:

explicit Command(T* pObj,void (T::*p_method)(),long timeout,const
char* key,long priority = PRIO_NORMAL ):
m_objptr(0),method(0),m_timeout(timeout),m_key(key),m_value(priority)
{
m_objptr=pObj;
method = p_method;
}

inline void execute()
{
(m_objptr->*method)();
}
//-----------------------------------------------------------------------

I use this as



where CTask is defined as

class CTask
{
void DoSomeTask()

Question 1
Is this usage safe?

It is not safe but it is legal.

Your execute methode could be safer:
inline void execute()
{
if( (m_objptr!=NULL) && (method!=NULL)
{
(m_objptr->*method)();
}
//else do nothing, throw an exception ...
}
ie will &CTask::DoSomeTask point to the method
of CTask ?
Yes.


I use this in a threadpool and I am sometimes getting thiings to work
and sometimes access violation (VC6 compiler)

Question 2
Is it possible that I can pass a parameter to the method

something like

class Command
{
T* m_objptr;
void (T::*method)(void *);


class CTask
{
void DoSomeTask(void *) or
static void DoSomeTask(void *)

If so how do you initiate Command class - I was getting compiler error
while doing this

The types are not the sames: &CTask::DoSomeTask is a void
(CTask::*)(void*) when a void (CTask::*)(void*) is expected.

You could use mem_fun structures for standard application but the
easiest is to use boost::function type boost::function0<void>.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top