在构造函数中将类对象强制转换为接口始终返回NULL [duplicate]

问题描述 投票:1回答:1

这个问题在这里已有答案:

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

界面:

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

班级:

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()应该检测到正确配置对象的接口...

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);

在构造函数中不起作用(总是返回NULL)...所以我需要稍后调用objInit() ...这不好。

UPDATE

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

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

MyServer(MqS *tmpl) : MqC(tmpl) {                                                                              
  objInit();                                                                                                   
};                                                                                                             
c++ constructor interface
1个回答
1
投票

void MqC::objInit () *this是一个MqCMqCIServerSetup没有任何关系,所以试图把它变成一个是不行的。

© www.soinside.com 2019 - 2024. All rights reserved.