Boost logo

Boost Users :

Subject: [Boost-users] [Signals2] connecting signals and pointers to objects
From: Julien Claassen (julien_at_[hidden])
Date: 2009-09-07 15:41:24


Dear all!
   I'm trying to let several threads communicate using Boost.Signals2. Now I
have encountered problems when working with pointers to my function-objects.
could you please read this example code and give me pointers to solving these
issues.

*** CODE EXTRACT ***
typedef boost::signals2::signal<void (char)> sig_t;

class Base
{
   public:
     boost::signals2::connection connect(const sig_t::slot_type&);
   protected:
     sig_t its_signal;
};

boost::signals2::connection Base::connect(const sig_t::slot_type& subscriber)
{
   return its_signal.connect(subscriber);
}

class Derived : public Base
{
   public:
     void run();
};

void Derived::run()
{
   // do something
   its_signal();
   // something more to be done
}

class Function_object
{
   public:
     void slot_func(char);
     void run();
};

void Function_object::slot_func(char c)
{
   cout << "Hurrah, I received char c: " << c << endl;
}

void Function_object::run()
{
   Base* my_base_pointer = new Derived();
   my_base_pointer->connect(boost::bind(&Function_object::slot_func, \
       this)); // Here I get errors from the Boost.Signals2 headers
   my_base_pointer->run();
}

int main()
{
   Function_object fo;
   thread taskrunner(boost::bind(&Function_object::run, &fo));
   taskrunner.join();
   return 0;
}
*** END OF CODE ***
   I compiled it with:
gcc (Debian 4.3.2-1.1) 4.3.2
   On Debian Lenny Linux.
   It seems I could have a reference to my Base or Derived object, when doing
the connection, but no pointer.
   The goal is to have more than one derived class from Base and store them all
in some kind of container or simple array. I tried making a vector of
references to the Base cass, but as it should be an abstract data type, it
didn't work.
   I would appreciate any help with this!
   Kindest regards
           Julien

--------
Music was my first love and it will be my last (John Miles)

======== FIND MY WEB-PROJECT AT: ========
http://ltsb.sourceforge.net
the Linux TextBased Studio guide
======= AND MY PERSONAL PAGES AT: =======
http://www.juliencoder.de


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net