c++ - 在构造函数中将类对象转换为接口(interface)总是返回 NULL

标签 c++ constructor interface

<分区>

我的使用接口(interface),如果在对象创建时使用了接口(interface),则需要信息:

界面:

class IServerSetup {
  public:
    virtual void ServerSetup () = 0;
}

类(class):

class MyServer : public MqC, public IServerSetup {                                                                 
  public:
    MyServer(MqS *tmpl) : MqC(tmpl) {};                                                                            

  private:
    // service to serve all incomming requests for token "HLWO"                                                    
    void MyFirstService () { 
      SendSTART();
      SendC("Hello World");                                                                                        
      SendRETURN();
    }

    // define a service as link between the token "HLWO" and the callback "MyFirstService"                         
    void ServerSetup() {                                                                                           
      ServiceCreate("HLWO", CallbackF(&MyServer::MyFirstService));                                                 
    }                                                                                                              
};                                                                                                                 

MqC 的构造函数:

MqC::MqC (struct MqS *tmpl) {                                                                          
  if (tmpl && (*(int*)tmpl) != MQ_MqS_SIGNATURE) {                                                             
    throw MqCSignatureException("MqS");                                                                        
  } else {
    hdl = MqContextCreate(0, tmpl);                                                                            
    MqConfigSetSelf (hdl, this);                                                                               
    this->objInit();    <<<<<<<<<<<< This is the important part…                                                                                     
  }                                                                                                            
}

现在 objInit() 应该检测接口(interface)以正确配置对象......

void MqC::objInit () {

    // use "hdl->setup.Parent.fCreate" to ckeck in context was initialized
    if (hdl->setup.Parent.fCreate != NULL) return;

    hdl->setup.Parent.fCreate = MqLinkDefault;
    hdl->setup.Child.fCreate = MqLinkDefault;

    // init the server interface
    IServerSetup * const iSetup = dynamic_cast<IServerSetup*const>(this);

    if (iSetup != NULL) {
      struct ProcCallS * ptr = (struct ProcCallS *) MqSysMalloc(MQ_ERROR_PANIC, sizeof(*ptr));
      ptr->type = ProcCallS::PC_IServerSetup;
      ptr->call.ServerSetup = iSetup;
      MqConfigSetServerSetup (hdl, ProcCall, static_cast<MQ_PTR>(ptr), ProcFree, ProcCopy);
    }
    ...

为了让它更短......行:

IServerSetup * const iSetup = dynamic_cast<IServerSetup*const>(this);

Does not work in a constructor (return always NULL)...所以我需要稍后调用objInit()...这是不好

更新

如果我在顶层构造函数中使用 objInit...这有效...

→ 但有没有可能避免这种情况(总是重复 objInit())...并在 MqC 构造函数中获取顶级对象?

MyServer(MqS *tmpl) : MqC(tmpl) {                                                                              
  objInit();                                                                                                   
};                                                                                                             

最佳答案

void MqC::objInit () *this 中是一个MqCMqCIServerSetup 没有任何关系,因此尝试将其转换为一个是行不通的。

关于c++ - 在构造函数中将类对象转换为接口(interface)总是返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875899/

相关文章:

javascript - 如何在 TypeScript 中实现具有一组值的接口(interface)?

c++ - C++ 中的列表列表

c++ - 如何将隐藏的数据发送到pyqt中的QComboBOX

javascript - 如何在 Google Closure 中使用 var_args 调用父类的构造函数?

javascript - 是否可以将值与构造函数的参数进行比较?

syntax - 关键字 var 后的下划线和接口(interface)名称是什么意思?

c++ - 来自仅消息窗口的电源状态更改通知

无法命名的 C++ 类型

c++ - 类(class)组成和施工顺序?

c# - 给定一个对象,我如何以编程方式告诉它支持哪些接口(interface)?