在构造函数处将类对象强制转换为接口始终返回 NULL
Cast a class object into an interface at constructor always returns NULL
我的类使用接口,如果在对象创建中使用了接口,则需要信息:
界面:
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()
...这不好。
更新
如果我在顶级构造函数中使用objInit
...这行得通...
→但是有没有可能避免这种情况(总是重复objInit()
(......并在MqC构造函数中获取顶级对象?
MyServer(MqS *tmpl) : MqC(tmpl) {
objInit();
};
Invoid MqC::objInit ()
*this
是一个MqC
.MqC
与IServerSetup
没有关系,因此试图将其转换为一个是行不通的。
相关文章:
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 如何在 c++ 中'NULL'字符串
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- c++使用foreach使数组为null
- 当使用通配符和null指针调用函数时,对输出的说明
- Visual C++GC接口如何启用它以及要包含哪个库
- Windows.h与GLFW.h的接口
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 提供与TMP和SFINAE的通用接口
- 如何在映射中返回null
- 为重写std::exception的库生成swig接口时出错
- 内联如何影响模块接口中的成员函数
- COM 接口 c# 封送数组数组
- 如何在 SCIP C++ 接口中获取 MILP 约束矩阵中的系数值
- 重载 -> shared_ptr 个实例中的箭头运算符<interface>,接口中没有纯虚拟析构函数
- 如何绑定 C++ gRPC 客户端的网络接口
- 为什么返回 NULL 不会破坏函数?
- 构造函数中的 QQuickItem 父项 null
- 模板化接口 - 创建一个泛型模板类以返回任何容器
- 在构造函数处将类对象强制转换为接口始终返回 NULL