Skip to content

03-member-arguments/main.cpp

An example of how to connect a member function with arguments to a KDBindings::Signal.

The output of this example is:

1
2
Bob received: Have a nice day!
Alice received: Thank you!
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
  This file is part of KDBindings.

  SPDX-FileCopyrightText: 2021-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Sean Harmer <sean.harmer@kdab.com>

  SPDX-License-Identifier: MIT

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include <kdbindings/signal.h>

#include <iostream>
#include <string>

using namespace KDBindings;

class Person
{
public:
    Person(std::string const &name)
        : m_name(name)
    {
    }

    Signal<std::string const &> speak;

    void listen(std::string const &message)
    {
        std::cout << m_name << " received: " << message << std::endl;
    }

private:
    std::string m_name;
};

int main()
{
    Person alice("Alice");
    Person bob("Bob");

    alice.speak.connect(&Person::listen, &bob);
    bob.speak.connect(&Person::listen, &alice);

    alice.speak.emit("Have a nice day!");
    bob.speak.emit("Thank you!");

    return 0;
}

Filename: 03-member-arguments/main.cpp


Updated on 2022-04-12 at 00:03:57 +0000

Back to top