问题描述
我正在实现类似于MFC运行时类的内容.
我的目标是使用这种类似的机制在运行时创建类的实例.我已经将"CreateObject"函数存储在一个结构中,但是,我被困在调用它的语法上!
该守则的摘要如下:-
Hi,
I am implementing something similar to the MFC Runtime Class.
My aim is to create an instance of a class, at runtime, using this similar mechanism. I have stored the ''CreateObject'' function in a structure, but, I''m stuck on the syntax to call it!
The synopsis of the Code is as follows:-
class CSgObj;
typedef CSgObj*(CSgObj::*PFNCREATE)(void);
struct SG_SRVR_RTC{
PFNCREATE pfnCreate;
};
CSgObj* CSgObj::_CreateNew(){\
return(CSgObj*)new CSgObj;\
}
// Initialize an Instance of an SG_SRVR_RTC
static SG_SRVR_RTC classCSgObj=
(PFNCREATE)(CSgObj::_CreateNew),
};
// Create a New object from the SG_SRVR_RTC
CSgObj* CSgObj::CreateNewObject(SG_SRVR_RTC * pRTC)
{
ASSERT(pRTC);
if(pRTC==NULL)return NULL;
try{
return this->*(pRTC->pfnCreate)();
}
catch(...){
ASSERT(0);
return NULL;
}
}
电话:
The Call:
return this->*(pRTC->pfnCreate)();
不切芥末.我应该在那里写什么才能调用此函数.
问候,
:)
谢谢,
我知道演员不是必需的,我一步一步调试到了那里,其中包含了演员的宏.
does not cut the mustard. What should I write there to call this function.
Regards,
:)
Thanks,
I Know the cast is not required, I got there by debugging step by step,the MACRO that contains it.
return (this->*(pRTC->pfnCreate))();
绝招.问题是在哪里放置星星,箭头和条纹.
谢谢!
does the trick. The question is where to put the stars, arrows and stripes.
Thanks!
推荐答案
static SG_SRVR_RTC classCSgObj=
(PFNCREATE)(CSgObj::_CreateNew),
};
{和&丢失,不需要强制转换.它可能很简单:
a { and an & are missing and the cast is not required. It could be as simple as:
static SG_SRVR_RTC classCSgObj=
{
&CSgObj::_CreateNew,
};
对于通话本身,最重要的括号缺失了.应该是:
And for the call itself, most important parenthesis are missing. It should be:
return (this->*(pRTC->pfnCreate))();
或者,如果您愿意,它们的位置也很糟糕,因为以下内容似乎也可以编译:
Or if you prefer, they are badly placed as the following seems to also compile:
return (this->*pRTC->pfnCreate)();
有意义的是()具有更高的优先级,并且在整个this->*pRTC->pfnCreate
表达式周围没有括号,括号将仅应用于它的直接左子表达式.也就是说,编译器将尝试先评估(pRTC->pfnCreate)()
,然后评估this->*
result_of_previous_expression .
实际上,它必须首先获取指向函数this->*pRTC->pfnCreate
的指针(这就是它必须放在括号中的原因),然后在该指针上调用该函数.
It make sense are () have higher priority and without parenthesis around the whole this->*pRTC->pfnCreate
expression, the parenthesis would be applied only to it immediate left sub expression. That is the compiler would try to evaluate (pRTC->pfnCreate)()
first and then this->*
result_of_ previous_expression.
In fact it must first get the pointer to the function this->*pRTC->pfnCreate
(this is why it must be in parenthesis) and then call the function on that.
这篇关于复杂的函数调用语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!