1

I am studying Drone Programming in C++. I found this function :

void Vehicle::_announceArmedChanged(bool armed)

But when I search for it, all the code calls this function without a parameter, such as:

connect(this, &Vehicle::armedChanged, this, &Vehicle::_announceArmedChanged);

In this case, where does the program get the armed status?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Alan Chew
  • 35
  • 1
  • 3
  • you might be looking at the definition while the declaration might declare a default parameter. See here: https://stackoverflow.com/questions/4989483/where-to-put-default-parameter-value-in-c – 463035818_is_not_an_ai Dec 08 '21 at 09:56
  • 1
    @463035818_is_not_a_number `connect` takes a member function as parameter ;) – YSC Dec 08 '21 at 09:56
  • 2
    wait a sec... `connect(this, &Vehicle::armedChanged, this, &Vehicle::_announceArmedChanged);` does not call the function. It passes a function pointer to `connect` the function is called elsewhere – 463035818_is_not_an_ai Dec 08 '21 at 09:57
  • 1
    That looks like a Qt signal/slot connection, which indeed just creates a pathway by which a signal emits a value and that is passed to a slot. But it does not execute that. Look for things like `emit armedChanged(true);` or other connections involving `armedChanged` that might chain the signal from somewhere else (_e.g._ a button "pressed" signal) – paddy Dec 08 '21 at 09:58
  • 1
    This is actually a 'Qt-specific' issue. You a registering a callback with this function. `anncounceArmedChanged` gets called each time, the signal `armedChanged` is triggered. And this call will contain the parameter of whatever is passed to the signal. – Refugnic Eternium Dec 08 '21 at 09:58
  • *In this case, where does the program get the armed status?* -- Look at the declaration of `connect`. That's the piece of the puzzle that's missing from your post, and would probably answer your question immediately. – PaulMcKenzie Dec 08 '21 at 10:03

3 Answers3

6

In this situation, announceArmedChanged() is a callback function. The code you showed is not calling announceArmedChanged(). It is passing the memory address of announceArmedChanged to QObject::connect() of the Qt framework, which will remember the address and associate it with the specified armedChanged event. When other code later triggers the armedChanged event with an appropriate status value, announceArmedChanged() will then be called with that value.

Read Signals & Slots in Qt's documentation for more details.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

"armedChanged" is a signal and this siagnal calls "_announceArmedChanged" slot when "emit armedChanged(true);" or "emit armedChanged(false);" Called. In this case both siganal and slot are in the same class;

-2

Pointers to member functions

A pointer to non-static member function f which is a member of class C can be initialized with the expression &C::f exactly. Expressions such as &(C::f) or &f inside C's member function do not form pointers to member functions.

https://en.cppreference.com/w/cpp/language/pointer

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
JsX
  • 1
  • 1
  • 2
    While technically useful, this really does nothing to answer the OP's question. – Remy Lebeau Dec 08 '21 at 10:09
  • The syntax of the pointer is not asked about. The fact that it is a pointer is part of the answer explanation, but the answer to "Where is `armed` coming from?" is not given at all. – Yunnosch Dec 08 '21 at 10:11