PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Command pattern problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Command pattern problem

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2007, 12h37   #1
alexcpn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Command pattern problem

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<CTask>(task1,&CTask:oSomeTask,task1-
>GetTimeOut(),"ne2_11"PRIO_NORMAL);


where CTask is defined as

class CTask
{
void DoSomeTask()

Question 1
Is this usage safe? ie will &CTask:oSomeTask 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<CTask>(task1,&CTask:oSomeTask(this) ,task1-
>GetTimeOut(),"ne2_11"PRIO_NORMAL);


Thanks for your time
Alex.C.P
  Réponse avec citation
Vieux 06/12/2007, 12h59   #2
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Command pattern problem

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
>
> cmd= new Command<CTask>(task1,&CTask:oSomeTask,task1-
>> GetTimeOut(),"ne2_11"PRIO_NORMAL);

>
> 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:oSomeTask 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
>
> cmd= new Command<CTask>(task1,&CTask:oSomeTask(this) ,task1-
>> GetTimeOut(),"ne2_11"PRIO_NORMAL);


The types are not the sames: &CTask:oSomeTask 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h46.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10699 seconds with 10 queries