简体   繁体   English

如何理解我在源代码中找到的这个C ++类声明?

[英]How interpret this C++ class declaration I found in a source?

I'm going through a piece of C++ source code for a personal project I'm working on and I can't for the world figure out what's happening in the following snippet: 我正在为我正在处理的个人项目编写一段C ++源代码,我不能让世界弄清楚以下代码片段中发生了什么:

Event EV_ScriptThread_Execute(
    "execute", EV_DEFAULT, NULL, NULL, "Execute the thread."
);

class ScriptThread : Listener
{
    { &EV_ScriptThread_Execute, Execute },
    { NULL, NULL}
};

void ScriptThread::Execute(Event *ev)    
{
    //Stuff
}

As far as I can interpret, it creates an instance of the Event-class and stores it in EV_ScriptThread_Execute . 据我所知,它创建了一个Event-class实例并将其存储在EV_ScriptThread_Execute It then defines the ScriptThread-class, but I have no idea how to interpret the line after it. 然后它定义了ScriptThread类,但我不知道如何解释它之后的行。

Could someone explain me what happens? 有人能解释一下会发生什么吗? How would { &EV_ScriptThread_Execute, Execute }, look like if it was written in full, and not, as it seems, a short-hand notation. { &EV_ScriptThread_Execute, Execute },看起来如果它是完整的,而不是,似乎是一个简写符号。

EDIT: Apparantly I misintepreted the macro-definition which I thought would produce: class ScriptThread : Listener , based on all the replies I went back and found out it actually construct an array of some sort. 编辑:显然我误解了我认为会产生的宏定义: class ScriptThread : Listener ,基于我回过头来的所有回复,发现它实际上构造了某种类型的数组。 It still kind of leaves me in the dark on what happens... as I can't actually compile the code (since it's just a snippet), I can't really look at the precompiled files either. 在发生的事情上,它仍然让我陷入黑暗......因为我实际上无法编译代码(因为它只是一个代码片段),我也无法真正查看预编译文件。

What is the result of this? 这是什么结果?

ResponseDef<ScriptThread> ScriptThread::Responses[] =
{
    { &EV_ScriptThread_Execute, Execute },
    { NULL, NULL}
};

Another EDIT: So I found ResponseDef is a struct which looks like this: 另一个编辑:所以我发现ResponseDef是一个结构,看起来像这样:

template< class Type >
struct ResponseDef
{
    Event *event;
    void (Type::*response)(Event *event);
};

Conclusively this means the array gets filled up with a structure which takes both a pointer to an instance of Event as well as a pointer to a method, which would explain the initializer! 最后,这意味着数组将填充一个结构,该结构既可以获取指向Event实例的指针,也可以获取指向方法的指针,这将解释初始化程序!

Thanks to everyone for brining me back on track! 感谢大家让我重回正轨!

Look for a macro definition for Listener. 查找Listener的宏定义。 I'm pretty sure that something horrible is hiding behind that. 我很确定隐藏着一些可怕的东西。 There is no other way to turn this snippet into something a C++ compiler will accept. 没有其他方法可以将此代码段转换为C ++编译器可以接受的内容。

I expect something like: 我希望有类似的东西:

#define Listener ScriptBaseClass { void Execute(Event *ev); } the_script[] =

Searching for the longest single token, EV_ScriptThread_Execute , turns up this: 搜索最长的单个令牌EV_ScriptThread_Execute ,会出现这样的情况:

Event EV_ScriptDoor_DoInit( "doinit" );   
Event EV_ScriptDoor_SetOpenThread( "openthread" );   
Event EV_ScriptDoor_SetCloseThread( "closethread" );   

ResponseDef ScriptDoor::Responses[] =   
{   
    { &EV_ScriptDoor_DoInit,           ( Response )ScriptDoor::DoInit },   
    { &EV_Door_DoClose,                  ( Response )ScriptDoor::DoClose },   
    { &EV_Door_DoOpen,                   ( Response )ScriptDoor::DoOpen },   
    { &EV_ScriptDoor_SetOpenThread,           ( Response )ScriptDoor::SetOpenThread },   
    { &EV_ScriptDoor_SetCloseThread,          ( Response )ScriptDoor::SetCloseThread },   
    { NULL, NULL }   
};

From here: .htm">http://read.pudn.com/downloads99/sourcecode/windows/bitmap/406853/doors.cpp_.htm 从这里:.htm“> http://read.pudn.com/downloads99/sourcecode/windows/bitmap/406853/doors.cpp_.htm

It's clearly related code, and what is it from? 它显然是相关的代码,它是什么来的? Quake 2. So that's what we're looking at--for anything more detailed about how or why your specific bit of code works, we'd need to see (or find) more...and that may be a bit tricky given the revision history listed in the above file: Quake 2.这就是我们所关注的 - 对于任何更详细的关于如何或为什么你的特定代码工作的东西,我们需要看到(或找到)更多......这可能有点棘手上述文件中列出的修订历史记录:

// 48    8/24/98 11:32a Markd   
// Added Start method to threads, repladed all ProcessEvent(   
// EV_ScriptThread_execute) with thread->Start( -1 )   

Maybe someone else knows who Markd is; 也许别人知道Markd是谁; I don't. 我不。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM