title: Passing in an Object to an abstract type's constructor in C++
body: <p>I'm trying to create CnD_Message_Handler of parent type i_MessageHandler. i_MessageHandler constructor takes a i_MessageFactory, another abstract class. CnD_Message_Factory inherits from i_MessageFactory. When I try to instantiate the CnD_Message_Handler, I get the following error: </p>
  
  <p><strong>error C2664: 'CnD_Message_Handler::CnD_Message_Handler' : cannot convert parameter 1 from 'CnD_Message_Factory' to 'const CnD_Message_Handler &amp;' 
  Reason: cannot convert from 'CnD_Message_Factory' to 'const CnD_Message_Handler'</strong></p>
  
  <p>From examples online, I believe I'm passing msg_factory correctly. I'm also confused as the constructor requests i_MessageFactory(CnD_Message_Factory) instead of i_MessageHandler(CnD_Message_Handler)</p>
  
  <p>Thanks for any help in advance! </p>
  
  <p>CnD_Device (which instantiates CnD_Message_Factory and CnD_Message_Handler)</p>
  
  <pre><code>CnD_Device::CnD_Device(void)
  {
    CnD_Message_Factory   msg_factory;                  //Inherited by i_MessageFactory 
    CnD_Message_Handler   msg_handler( msg_factory ); 
  }
  </code></pre>
  
  <p>CnD_Message_Factory</p>
  
  <pre><code>#include "i_messagefactory.h"
  
      class CnD_Message_Factory :
        public i_MessageFactory
      {
      public:
        CnD_Message_Factory(void);
        ~CnD_Message_Factory(void);
  
          /**
           * Creates a message using the stream of data passed in.
           * @param id Id of the message to create.
           * @param stream Data stream to create the message from.
           * @return The created message (which must be returned to the factory by
           * calling the deleteMessage() method, or null if the factory could not
           * create a message.
           */
          Message* createMessage(UInt32 id, const char* stream);
  
          /**
           * Returns a message to the factory for deleting/recycling.
           * @param msg The message being returned.
           */
          void deleteMessage(Message&amp; msg);
      };
  </code></pre>
  
  <p>CnD_Message_Handler</p>
  
  <pre><code>#include "i_messagehandler.h"
  
  class CnD_Message_Handler :
    public i_MessageHandler
  {
  public:
  
  
    CnD_Message_Handler::~CnD_Message_Handler(void);
  
  /**
  * Called by a i_MessageDriver object to process a message received.
  * @param msg Message to process.
  */
  void  CnD_Message_Handler::handleMessage (Message&amp; msg);
  
  /**
  * Called by a i_MessageDriver object when an error occurs with an
  * interface  The exact type of errors are driver specific.
  * @param error The error that occurred.
  */
  void  CnD_Message_Handler::handleError (MessageEvent&amp; error);
  
  /**
  * Called by the i_MessageDriver object when an event occurs with an
  * interface.  The exact type of events are driver specific.
  * @param event The event that occurred.
  */
  void  CnD_Message_Handler::handleEvent (MessageEvent&amp; event);
  };
  </code></pre>
  
  <p>i_MessageHandler</p>
  
  <pre><code> class  i_MessageFactory
  {
    public:
  
      /**
       * Destructor.
       */
      virtual ~i_MessageFactory(void) { }
  
      /**
       * Creates a message using the stream of data passed in.
       * @param id Id of the message to create.
       * @param stream Data stream to create the message from.
       * @return The created message (which must be returned to the factory by
       * calling the deleteMessage() method, or null if the factory could not
       * create a message.
       */
      virtual Message* createMessage(UInt32 id, const char* stream) = 0;
  
      /**
       * Returns a message to the factory for deleting/recycling.
       * @param msg The message being returned.
       */
      virtual void deleteMessage(Message&amp; msg) = 0;
  
  
    protected:
  
      /**
       * Constructor.
       */
      i_MessageFactory(void) { }
  };
  </code></pre>