1

I have this code:

// signal supporter parent
class signalable {};

template <class typeT = signalable>
typedef void (typeT::*trig)(std::string);

template <class typeT = signalable>
class trigger
{
    private:
        typeT* instance;
        typeT::trig fun;

    public:
        trigger(typeT* inst, typeT::trig function)
            : instance(inst), fun(function)
        {}
        void operator ()(std::string param)
        {
            (instance->*fun)(param);
        }
};

And I get lots of compile error that I bet pros know of. I'm just confused a little bit about this context.

What I want to do is clear: Pass pointer to an object, and pointer to one of it's member functions, to make a functor and pass it over in my program.

Would appreciate your helps and "corrections".

Thank you!

Haix64
  • 748
  • 1
  • 12
  • 20
  • 2
    Ben C answered your question, but you might want to look into the [Observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) for a nice approach to what I think is your **original** problem. – smocking Apr 21 '12 at 11:42
  • @smocking I appreciate your point, a case I've dealt with before, but in this case the observer pattern my aim, since in my code events are to be propagated in a "chained" style rather than some event be propagated to multiple callees. Anyway, thanks for helping me observe the contrast. – Haix64 Apr 21 '12 at 12:01
  • I'm not sure I understand why you would want that sort of signalling, but couldn't you extend the pattern a little bit by having your "chained" signalling classes inherit from both an Observer and Observable interface? – smocking Apr 21 '12 at 12:43
  • I guess that's gotta be the right approach, though in this very small project I'm working on that more than half of concepts are hard coded, it's not of such a level of importance to apply such enterprise scale solutions. I appreciate this comment of yours too. Thanks. – Haix64 Apr 21 '12 at 13:13

1 Answers1

0

Are you trying to do something like this?

#include <string>
#include <iostream>

// signal supporter parent
class signalable 
{
public:
    void foo(std::string s) { std::cout << "hello: " << s << std::endl; }
};


template <class typeT = signalable>
class trigger
{

    typedef void (typeT::*trig)(std::string);

    private:
        typeT* instance;
        trig fun;

    public:
        trigger(typeT* inst, trig function)
            : instance(inst), fun(function)
        {}
        void operator ()(std::string param)
        {
            (instance->*fun)(param);
        }
};

int main()
{
    signalable s;
    trigger<> t(&s, &signalable::foo);
    t("world");
}

As for some of the more specific errors in your code, most of them seem to relate to your typedef. C++11 allows "template typedefs", but they don't look like that. Have a look at this thread for an example of template typedefs:

C++ template typedef

Community
  • 1
  • 1
Ben Cottrell
  • 4,680
  • 1
  • 23
  • 31
  • Your code is correct Ben. Simply changing member `fun`'s and constructor's `typeT::trig` to `trig` solved it. Thank you. – Haix64 Apr 21 '12 at 11:36