在C ++中将Object传递给抽象类型的构造函数

时间:2011-10-03 14:45:22

标签: c++ abstract-class c2664

我正在尝试创建父类型为i_MessageHandler的CnD_Message_Handler。 i_MessageHandler构造函数接受另一个抽象类i_MessageFactory。 CnD_Message_Factory继承自i_MessageFactory。当我尝试实例化CnD_Message_Handler时,我收到以下错误:

错误C2664:'CnD_Message_Handler :: CnD_Message_Handler':无法将参数1从'CnD_Message_Factory'转换为'const CnD_Message_Handler&' 原因:无法从'CnD_Message_Factory'转换为'const CnD_Message_Handler'

从网上的例子来看,我相信我正确地传递了msg_factory。我也很困惑,因为构造函数请求i_MessageFactory(CnD_Message_Factory)而不是i_MessageHandler(CnD_Message_Handler)

提前感谢您的帮助!

CnD_Device(实例化CnD_Message_Factory和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) { }
};

3 个答案:

答案 0 :(得分:2)

  

CnD_Message_Handler不会重新定义构造函数。

构造函数不是在C ++ 03中“继承”的。您需要为从中继承的所有类型提供构造函数参数。这是一个例子。

struct Arg {};

struct Foo {
  Foo(Arg arg) {}
  virtual ~Foo() {}
};

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

它们可以继承C ++ 11,但需要特殊的语法。

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

答案 1 :(得分:1)

CnD_Message_Handler没有用户定义的构造函数。相反,它试图使用编译器免费提供的复制构造函数,并告诉你它无法将传入的工厂转换为编译器提供的复制构造函数所期望的const CnD_Message_Handler&

只需为CnD_Message_Handler定义构造函数即可获取工厂并实例化其基类:

CnD_Message_Handler(i_MessageFactory& foo) : i_MessageHandler(foo) {}

答案 2 :(得分:0)

CnD_Message_Handler定义的构造函数是什么?你需要一个(引用一个)i_MessageFactory(或CnD_Message_Factory)。如果没有,它将尝试自动生成的构造函数,如复制构造函数。而且我认为这就是这里发生的事情。