简体   繁体   English

c ++类访问关键字“信号”是什么意思?

[英]What does the c++ class access keyword “signals” mean?

I came across a declaration similar to this (names changed per NDA ): 我遇到了类似于此的声明(根据NDA更改了名称):

class Foo
{
    int  bar;

 public:
    explicit Foo (Fu *parent = NULL);

 private:
    void somefunc (String);

 signals:        // ??? what does this do ???
    void windowClosed();
};

This is compiled successfully by g++ 4.4.7 (from about 2012). 这是由g ++ 4.4.7(从大约2012年)成功编译的。 Additionally, vim recognizes it as a keyword similar to public and private by highlighting them in brown. 此外,vim通过以棕色突出显示它,将其识别为类似于publicprivate的关键字。 (Dis)similiarly, vim uses green to highlight the keywords namespace , class , void , int , double , float , char , unsigned , etc. (Dis)同样,vim使用green来突出显示关键字namespaceclassvoidintdoublefloatcharunsigned等。

The Stackoverflow code formatter does not highlight signals above like it does public and private ! 在#1代码格式化并不突出signals上述像它publicprivate

It has proved quite difficult to Google for this (lots of noise), but I haven't found anything mentioning it, not even on SO. 事实证明谷歌很难(很多噪音),但我没有发现任何提及它,甚至没有提到它。 I also looked in the enhancements section of the g++ documentation. 我还查看了g ++文档的增强功能部分

This codebase is large (23+ million lines), oldish (~1998), and has a distinct accent. 这个代码库很大(2300万行),古老(〜1998),并且有明显的重音。 For example, the same class definition has the class access private slots: before two member functions. 例如,相同的类定义具有类访问private slots:在两个成员函数之前。 So it is possible there is some #define obfuscation or trickery going on, but I can't find it using grep . 所以有可能会出现一些#define混淆或欺骗,但我无法使用grep找到它。 It is possible g++ has been altered, but its --version output does not indicate modification. g ++可能已被改变,但其--version输出并不表示修改。

It isn't a keyword, if you're using a framework such as Qt it adds up 'keywords' to the language to provide additional functionality that isn't part of the standard. 它不是关键字,如果您使用的是Qt这样的框架,它会在语言中添加“关键字”,以提供不属于标准的其他功能。

In Qt for example this code gets preproccessed by the MOC(Meta-Object Compiler), signals is defined in qobjectdefs.h as #define signals public , the moc checks for this macro to add meta-code and provide the actual signal-slot functionality, the generated code is put into a file with a name such as 'moc_myClass.cpp'. 例如,在Qt中,此代码由MOC(元对象编译器) qobjectdefs.hsignalsqobjectdefs.h定义为#define signals public ,moc检查此宏以添加元代码并提供实际的信号槽功能,生成的代码被放入一个名为'moc_myClass.cpp'的文件中。

These are not c++ keywords! 这些不是c ++关键字! c++ will not compile them, unless you include a header from the Qt library which will remove them with the preprocessor using: c ++不会编译它们,除非你从Qt库中包含一个标题,它将使用预处理器删除它们:

//from qobjectdefs.h
#     define slots
#     define signals public

These are "tags" used by the Qt Library Preprocessor (moc, Meta Object Compiler) for processing them. 这些是Qt库预处理器(moc,Meta Object Compiler)用于处理它们的“标签”。

They mark methods which are used in a signal-slot communication mechanism between objects. 它们标记在对象之间的信号槽通信机制中使用的方法。 moc will process them only if the class is marked with the Q_OBJECT macro, and these classes must inherit from QObject. 只有在使用Q_OBJECT宏标记类时,moc才会处理它们,并且这些类必须从QObject继承。

From these, moc create a companion class in moc_xxxxx.h and moc_xxxxx.cpp source files, with the necessary code for handling the signal/slot mechanism. 从这些中,moc在moc_xxxxx.h和moc_xxxxx.cpp源文件中创建一个伴随类,其中包含处理信号/插槽机制所需的代码。

Even if you have Qt headers and libraries, you will generally not be able to link the code without the qmake generated sources. 即使您有Qt标头和库,如果没有qmake生成的源,您通常也无法链接代码。

signals is a macro defined by the Qt framework that is translated to public during compilation while serving as a beacon to Qt's moc preprocessor for implementing observer-like class relations with "signals" and "slots". signals是由Qt框架定义的宏,在编译期间转换为public ,同时作为Qt的moc预处理器的信标 ,用于实现与“signals”和“slots”类似观察者的类关系。 Other similar Qt macros include slots and Q_SLOTS . 其他类似的Qt宏包括slotsQ_SLOTS

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

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