Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the use of a C++ class containing an "implementation" and "interface" pointer to itself?
    primarykey
    data
    text
    <p>I'm studying some source codes and I'm just wondering why a class (or classes) is often implemented this way:</p> <pre><code> class EventHandler { ... EventDispatcher* m_Dispatcher; virtual bool ProcEvent( EventHandler* Sender, int Task, int Event, void* Param ); ... }; class ClassA: public EventHandler { ... ClassA* m_Impl; ClassA* m_Iface; ... public: // virtual functions virtual bool ProcEvent( EventHandler* Sender, int Task, int Event, void* Param ); virtual void OnDataWritten(const PIOResult&) {;} ... // sample functions void SetImplement( ClassA* aImpl ); void SetInterface( ClassA* aIface ); ClassA* GetImplement() { return m_Impl; } ClassA* GetInterface() { return m_Iface; } bool GetData( list& aList ); }; // Implementation of some sample functions; Most of its function contain more // or less the same format as below, with the return m_Impl->XXX having the same // name as the function being defined (e.g. A::XXX) bool ClassA::GetData( list&lt Data &gt& aList ) { if( m_Impl ) return m_Impl->GetData( aList ); else return false; } class ClassAFactory: public EventHandler { private: ClassAFactory* m_Impl; ClassAFactory* m_Iface; protected: virtual ClassA* MakeTransport(); virtual bool ProcEvent( EventHandler* Sender, int Task, int Event, void* Param ); virtual ClassA* CreateClassA() { return 0; } ... }; // In some member function of ClassB (ClassB inherits ClassA) switch( status ) { case 1: GetInterface()->OnDataWritten(); case 2: // ... }; </code></pre> <p>I believe it's for some design pattern but I'm not familiar with it. It could help me understand if I know what it is. Can anyone help me point out which it could be or what is the use of these classes such that it is implemented this way?</p> <p>I think it's for some event handling and used together with some factory but I'm not sure.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload