|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I want to find out the best "C++" way to do this: I have 2 classes: class IHost: { static callback(..) } and #include "IHost.h" class PluginManager: { .... protected: IHost m_IHost; } For one function, the IHost class has to signal back to the PluginManager class that the plugin has signalled a status change. Before I get into what I tried, I want to first ask, what is the proper "C++ way" of doing such a circular reference? Friend class? Anyway, I tried this: #include "PluginManager.h" class IHost: { IHost ( PluginManager *ptr ) { m_pPluginManager = ptr; } static callback(..) protected: PluginManager *m_pPluginManger } and #include "IHost.h" class PluginManager: { .... protected: IHost m_IHost; } However, my compiler still won't recognize PluginManager as a class in the IHost class definition. I've had a problem in the past that if I try to include in a class a class that is already including that class (like the example directly above), the compiler barfs in C++. My next thought was to try a friend class definition, but then I figrued I would try and find out the proper way to do this. Thanks for any feedback in advance. T |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Nevermind, I think I figured it out:
class PluginManager; class IHost: { IHost ( PluginManager *ptr ) { m_pPluginManager = ptr; } static callback(..) protected: PluginManager *m_pPluginManger } and #include "IHost.h" class PluginManager: { .... protected: IHost m_IHost; } |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Dec 29, 12:01 pm, TBass <t...@automateddesign.com> wrote:
> Hi, > > I want to find out the best "C++" way to do this: > > I have 2 classes: > > class IHost: > { > > static callback(..) > > } > > and > > #include "IHost.h" > class PluginManager: > { > .... > > protected: > IHost m_IHost; > > } > > For one function, the IHost class has to signal back to the > PluginManager class that the plugin has signalled a status change. > Before I get into what I tried, I want to first ask, what is the > proper "C++ way" of doing such a circular reference? Friend class? > > Anyway, I tried this: > > #include "PluginManager.h" > class IHost: > { > IHost ( PluginManager *ptr ) > { > m_pPluginManager = ptr; > } > > static callback(..) > > protected: > PluginManager *m_pPluginManger > > } > > and > > #include "IHost.h" > class PluginManager: > { > .... > > protected: > IHost m_IHost; > > } > > However, my compiler still won't recognize PluginManager as a class in > the IHost class definition. I've had a problem in the past that if I > try to include in a class a class that is already including that class > (like the example directly above), the compiler barfs in C++. it should barf > > My next thought was to try a friend class definition, but then I > figrued I would try and find out the proper way to do this. > > Thanks for any feedback in advance. > > T Its always full if you post compileable code, even if its toy code. Use initialisation lists in your constuctors, other languages would kill to have them. Your problem involves forward declarations: // forward declaration class PluginManager; class IHost { PluginManager* m_pPluginManger; public: IHost(PluginManager* ptr) : m_pPluginManger(ptr) { } static void callback() { } }; class PluginManager { public: PluginManager() : m_IHost(this) { } protected: IHost m_IHost; }; int main() { PluginManager pm; } As far as callbacks are concerned, take a look at Boost.bind, Boost.Function |
|
![]() |
| Outils de la discussion | |
|
|