复杂的函数调用语法 [英] Complicated Functin Call Syntax
问题描述
我正在实现类似于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!
推荐答案
在该代码段中:
In that code fragment:
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.
好吧,这不是解决方案,而是在呼救!
坦率地说,我根本不了解如何管理此论坛格式!我问一个问题,收到大量电子邮件,只有最后一个(最完善的)答案停留在网站上!如果在我不在的情况下他们的答案被更好的答案覆盖,我应该投票给人们(如何?).我得到的答案可能经常使我重新思考所提出的问题.解答任何问题的途径通常都是围绕问题的层次进行的.当我改进一个问题或改进一个答案时,到达某物的整个思路就迷路了!
这种论坛格式的唯一用途是,我需要提醒我曾经学过的东西,但此后却被遗忘了.
问候:)
Well, Not a Solution, but a Cry for Help!
I frankly do Not understand how to manage this forum format At All! I Ask a question, get a number of emails, and only the last (most improved)answer sticks on the site! I am supposed to vote for people (how?) if their answer gets overwritten by a better answer in my absence. The answers I get, may often make me re-think the question asked. The route to any answer often follows an hiarchie of questions. When I improve a question, or Improve an answer, the whole thread of thought of arriving at something gets lost!
The Only use of a forum format of this kind is, where I need a reminder of something I once learned, but have since forgotten about.
Regards :)
这篇关于复杂的函数调用语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!