Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the c++ class access keyword "signals" mean?

I came across a declaration similar to this (names changed per 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). Additionally, vim recognizes it as a keyword similar to public and private by highlighting them in brown. (Dis)similiarly, vim uses green to highlight the keywords namespace, class, void, int, double, float, char, unsigned, etc.

The Stackoverflow code formatter does not highlight signals above like it does public and private!

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.

This codebase is large (23+ million lines), oldish (~1998), and has a distinct accent. For example, the same class definition has the class access private slots: before two member functions. So it is possible there is some #define obfuscation or trickery going on, but I can't find it using grep. It is possible g++ has been altered, but its --version output does not indicate modification.

like image 740
wallyk Avatar asked Apr 01 '15 17:04

wallyk


People also ask

How do Qt signals work?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

What is signals and slots in Qt?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

How do I turn off Qt signal?

disconnect(& myConnect);

Why do we need signals and slots when we can directly call 2 objects using their member functions?

A public slots section contains slots that anyone can connect signals to. This is very useful for component programming: you create objects that know nothing about each other, connect their signals and slots so that information is passed correctly, and, like a model railway, turn it on and leave it running.


1 Answers

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.

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'.

like image 186
Alejandro Díaz Avatar answered Sep 28 '22 07:09

Alejandro Díaz