Mac Mac - 1 month ago 4x
C++ Question

Why are QT signals NOT const

Qt uses signals and slots for object communication. Signals are normally declared as a member function and the QT MOC then generates the definition of that function.

What I would like to understand is why signals are not const member functions?

I would expect signals not to modify the sender, that's why the question.


I would expect signals not to modify the sender

Signals (as generated by the MOC) do not directly modify a class instance' members. The generated code, however, passes a this pointer along, for consumption by a (potential) slot. A connected slot could thus mutate the sender of the signal.

So the technical reason is, that if signals were const, it would require that all slot implementations would only call const class members on the sender for the code to compile without errors.

Implementing signals as non-const class members is an understandable decision, with respect to code safety. It still feels unnatural in a number of cases (e.g. if the connected slot implemented in the same class is const, or if the connected slot belongs to another object altogether).