If I have two classes:
class bar
{
public:
void RunTheBar(void);
};
class foo
{
private:
bar bar_;
public:
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
...
};
What's the best way to design "bar" such that it can call
foo::Callback()
from within its RunTheBar() method?
This is pretty straightforward using the Borland C++Builder extension
"__closure":
typedef void (__closure *MyCallback)(void);
class bar
{
private:
MyCallback callback_;
public:
bar(void):callback_(0){}
void RunTheBar(void);
void SetCallback(MyCallback Acallback){callback_ = Acallback;}
};
void bar::RunTheBar(void)
{
// ...
if (callback_)
{
callback_();
}
// ...
}
class foo
{
private:
bar bar_;
public:
foo(void)
{
bar_.SetCallback(Callback);
}
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
};
Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.
--
Simon Elliott
http://www.ctsn.co.uk/