Recently i have come up with the scenario where i need a single class to handle a few different interfaces and i have found a few different approaches to take. I was wondering if anyone has encountered the same situation and can give me any suggestion(s) as to if there is a better approach i could take here?

The Simple solution would be

/* EventObserver.h */
class EventObserver
{
public:
    virtual ~EventObserver();
    virtual void onEvent( Observable& src ) = 0;
};

/* NetworkMessageHandler.h */
class NetworkMessageHandler
{
public:
    virtual ~NetworkMessageHandler();
    virtual void onNetworkData( const char* pszBuff, size_t nSize ) = 0;
};


/* RemoteApplicationManager.h */
#include "NetworkMessageHandler.h"
#include "EventObserver.h"
class RemoteApplicationManager : public EventObserver, public NetworkMessageHandler
{
public:
    virtual ~RemoteApplicationManager();

    virtual void onEvent( Observable& src );
    virtual void onNetworkData( const char* pszBuff, size_t nSize );
};

I have a few issues with this "simple" approach:

  1. There may be name conflicts in the interface method names and
  2. I dont really want any other code to call the methods.
  3. Encapsulation - i want to be able to use only forward declarations of all class names external to my class. as including many *.h files causes slowing of the build process ridiculous dependency chains and greatly reduces pollution of namespaces from cross included headers.

So the approach i have developed recently is an adaptation of the pimpl idiom and the code would look more like this:

/* RemoteApplicationManager.h */
class EventObserver;
class NetworkMessageHandler;

class RemoteApplicationManager
{
public:
    virtual ~RemoteApplicationManager();

    EventObserver* getEventObserver() const;
    NetworkMessageHandler* getNetworkMessageHandler() const;

private:
    class EventObserverImpl;
    class NetworkMessageHandlerImpl;

    EventObserverImpl* m_pEventObserver;
    NetworkMessageHandlerImpl* m_pNetworkMessageHandler;
};

/* RemoteApplicationManager.cpp */
#include "NetworkMessageHandler.h"
#include "EventObserver.h"

class RemoteApplicationManager::EventObserverImpl
{
public:
    EventObserverImpl( RemoteApplicationManager& parent ); /* parent reference included to give access to super class internals */
    virtual ~EventObserverImpl();

    virtual void onEvent( Observable& src );

private:
    RemoteApplicationManager& m_parent;
}

class RemoteApplicationManager::NetworkMessageHandlerImpl
{
public:
    NetworkMessageHandlerImpl( RemoteApplicationManager& parent ); /* parent reference included to give access to super class internals */
    virtual ~NetworkMessageHandlerImpl();

    virtual void onNetworkData( const char* pszBuff, size_t nSize );

private:
    RemoteApplicationManager& m_parent;
}

The major flaws in this approach are:
1. Interacting with the containing class is messy as you always have to go through a parent reference for access to its variables / methods.
2. Fully qualifying the implementation methods can make for some seriously long lines of code which is a bit ugly, but i can live with that.

So to reiterate then, has anyone else encountered this situation and found a neat way to handle this in C++? Ideally id like to get close to the ease of use that java has for declaring implementations of an interface on the fly, but i dont think thats possible due to lanugage differences.

Java example:

NetworkMessageHandler = new NetworkMessageHandler()
{
    void onEvent( Observable src )
    {
        /* do stuff */
    }
};

Thanks in advance.

Edited 1 Year Ago by Kanoisa: fixed code listing

Oh woah, thats a really elegant solution there, thanks.

However it reminded me of one thing, in the case where i needed this i did not have C++11 at my disposal, but i did have boost so i can replace the c11 calls with boost calls.

This question has already been answered. Start a new discussion instead.