| View previous topic :: View next topic |
| Author |
Message |
David Bailey Guest
|
Posted: Mon Jun 19, 2006 6:46 am Post subject: Pointer to a callback function |
|
|
Help!
Hi,
A question previously asked gave the answer of the definititon of a callback
function as being:
typedef void __fastcall (__closure *OnMMMTimerEvent)(TObject *Sender);
I need to initialise a pointer to a callback function:
void __fastcall (__closure **Callback)(int x);
How would I do it? eg
void __fastcall (__closure **Callback)(int x) = new ?
Thanks in advance,
David |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Jun 19, 2006 7:51 am Post subject: Re: Pointer to a callback function |
|
|
"David Bailey" <dbai7864 (AT) bigpond (DOT) net.au> wrote in message
news:4495fe8c (AT) newsgroups (DOT) borland.com...
| Quote: | I need to initialise a pointer to a callback function:
void __fastcall (__closure **Callback)(int x);
|
Why are you trying to use a pointer to a pointer?
| Quote: | How would I do it?
|
Simply use the typedef to create your variable, ie:
void __fastcall TSomeClass::DoIt(TObject *Sender)
{
}
void __fastcall TSomeClass::DoSomething()
{
OnMMMTimerEvent Callback = DoIt;
//...
}
Or as a class method:
class TSomeClass
{
public:
OnMMMTimerEvent Callback;
};
void __fastcall TSomeOtherClass::DoSomething()
{
TSomeClass cls;
cls.Callback = DoIt;
//...
}
void __fastcall TSomeOtherClass::DoIt(TObject *Sender)
{
//...
}
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Jun 19, 2006 8:10 am Post subject: Re: Pointer to a callback function |
|
|
"David Bailey" <dbai7864 (AT) bigpond (DOT) net.au> wrote in message
news:4496241a$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Sorry, but I could not follow your example with what I am trying to do.
|
That is because you are trying to use too many levels of indirection when
you don't need to.
| Quote: | Esentially, I need a place holder for a pointer to a callback method.
|
I've already shown you what you should be doing instead, but here is the
corrected version of your example:
ClassA
{
private:
int Counter;
public:
ClassA();
void __fastcall CallBack(int a);
void __fastcall MasterMethod(void);
};
typedef void __fastcall (__closure *CallBackMethod)(int);
ClassB
{
private:
CallBackMethod CallBack;
public:
ClassB();
void __fastcall Transverse(CallBackMethod callBack);
};
ClassA::ClassA()
{
Counter = 0;
}
void __fastcall ClassA::CallBack(int a)
{
Counter += a;
}
void __fastcall ClassA::MasterMethod(void)
{
ClassB cb;
cb.Transverse(CallBack);
}
ClassB::ClassB()
{
CallBack = NULL;
}
void __fastcall Transverse(CallBackMethod callBack)
{
CallBack = callBack;
}
Gambit |
|
| Back to top |
|
 |
David Bailey Guest
|
Posted: Mon Jun 19, 2006 8:10 am Post subject: Re: Pointer to a callback function |
|
|
Hi Gambit,
Sorry, but I could not follow your example with what I am trying to do. To
try and make it clearer, here is some pseudo code with the basic intention.
Esentially, I need a place holder for a pointer to a callback method.
ClassA
{
private:
int Counter;
public:
__fastcall ClassA()
{
Counter=0;
};
void __fastcall CallBack(int a)
{
Counter+=a;
};
void __fastcall MasterMethod(void)
{
ClassB* cb = new ClassB();
cb->Transverse(CallBack);
delete cb;
};
}
ClassB
{
private:
void __fastcall (__closure ** CallBackMethod)(int a);
public:
__fastcall ClassB()
{
// how do I initialise the following statement?
//void __fastcall (__closure ** CallBackMethod)(int a)
*CallBackMethod=NULL;
};
void __fastcall Transverse(void __fastcall (__closure *
callBackMethod)(int a))
{
*CallBackMethod = callBackMethod;
}
}
David. |
|
| Back to top |
|
 |
|