Can I have a class member variable that points to a function in another class?

The goal to create a button class that holds the function that is supposed to be invoked in another class when the button is clicked in a member variable. This may seem a bit strange but it would really help to create a dynamic toolbar class that is to be used by plugins. The button that gets added to the toolbar needs to invoke a method inside the plugin.

So I need to know if it is possible to have a class member variable that points to a function in another class and how I would go about declaring that variable. I think I have already seen how to pass that function pointer as a function parameter and how to invoke it inside a function... this is probably related but I have never seen anything like this in a class definition.
> The goal to create a button class that holds the function
> that is supposed to be invoked in another class when the button is clicked ...
> This may seem a bit strange

It is not strange; the command design pattern comes in handy.
http://sourcemaking.com/design_patterns/command

There are several ways to implement this.
Links to Java (Qt/SMFL/other dreadful library) style implementations are there in the page cited earlier.
(see: Code examples/C++ at the bottom of the page).

The canonical C++ style is a non-intrusive style. Something along these lines:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <functional>
#include <vector>
#include <string>

struct button /* button_base */
{
    const int id ;
    static int next_id ;

    button() : id(next_id++) {}

    template < typename CALLABLE, typename... ARGS >
    explicit button( int id, CALLABLE&& fn, ARGS&&... args ) : id(id)
    { 
        if( id > next_id ) next_id = id + 1 ; 
        subscribe( std::forward<CALLABLE>(fn), std::forward<ARGS>(args)... ) ;
    }

    template < typename CALLABLE, typename... ARGS >
    void subscribe( CALLABLE&& fn, ARGS&&... args )
    { commands.emplace_back( std::bind( std::forward<CALLABLE>(fn), std::forward<ARGS>(args)... ) ) ; }

    /* virtual */ void click()
    {
        std::cout << "*** button::click: button #" << id << " is clicked.\n" ;
        for( const auto& cmd : commands ) cmd() ;
    }

    std::vector< std::function< void() > > commands ;
};

int button::next_id = 1 ;

void foo( int i ) { std::cout << "::foo( " << i << " )\n" ; }

int bar( std::string tag, std::string info  )
{
    std::cout << "::bar( '" << tag << "', '" << info << "' )\n" ;
    static int call_cnt = 0 ;
    return ++call_cnt ;
}

int main()
{
    struct A
    {
        void mfun( double d ) const
        { std::cout << "A::mfun( " << this << ", " << d << " )\n" ; }
    } a;

    button my_buttons[] =
    {
        button( 19, foo, 19 ),
        button( 26, bar, "from button twentysix", "hello" ),
        button( 15, &A::mfun, std::ref(a), 15.15 ),
        button()
    } ;

    for( button& b : my_buttons ) b.click() ;
    std::cout << "--------------------\n" ;

    const auto clicked = []( button& b )
    { std::cout << "main::closure0: button with id " << b.id << " was clicked\n" ; } ;

    for( button& b : my_buttons ) b.subscribe( clicked, b ) ;
    my_buttons[1].subscribe( [] { std::cout << "main::closure1: one more subscription for this button\n" ; } ) ;
    for( button& b : my_buttons ) b.click() ;
}

http://coliru.stacked-crooked.com/a/fbe0e4d0db8e9938
Thanks for answering this question.... I had decided to ask it in the Qt forum since I thought of a Qt specific possibility which is why I never checked back here.
Topic archived. No new replies allowed.