Search code examples
c++abstract-classc2664

Passing in an Object to an abstract type's constructor in C++


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:

error C2664: 'CnD_Message_Handler::CnD_Message_Handler' : cannot convert parameter 1 from 'CnD_Message_Factory' to 'const CnD_Message_Handler &' Reason: cannot convert from 'CnD_Message_Factory' to 'const CnD_Message_Handler'

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)

Thanks for any help in advance!

CnD_Device (which instantiates CnD_Message_Factory and CnD_Message_Handler)

CnD_Device::CnD_Device(void)
{
  CnD_Message_Factory   msg_factory;                  //Inherited by i_MessageFactory 
  CnD_Message_Handler   msg_handler( msg_factory ); 
}

CnD_Message_Factory

#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& msg);
    };

CnD_Message_Handler

#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& 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& 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& event);
};

i_MessageHandler

 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& msg) = 0;


  protected:

    /**
     * Constructor.
     */
    i_MessageFactory(void) { }
};

Solution

  • CnD_Message_Handler does not redefine the constructor.

    Constructors are not "inherited" in C++03. You are required to provide constructor arguments for all types you inherit from. Here is an example.

    struct Arg {};
    
    struct Foo {
      Foo(Arg arg) {}
      virtual ~Foo() {}
    };
    
    struct Bar : public Foo {
      Bar(Arg arg) : Foo(arg) {}
    };
    

    They can be inherited C++11, but require special syntax.

    struct Bar : public Foo {
      using Foo::Foo;
    };