On a project I’ve been working on I’ve made use of boost’s signals2. This worked fine untill I started making use of virtual classes. Now slots don’t get called anymore. The following is a stripped down version of my project.
Class Glue creates instances of classes VSig and Process and connects the signal in VSig to the slot in Process.
IVSig.h:
class IVSig {
public:
boost::signals2::signal<void (void)> sigTest;
virtual void update() = 0;
};
VSig.h:
class VSig : public IVSig {
public:
boost::signals2::signal<void (void)> sigTest;
void update();
};
VSig.cpp:
void VSig::update()
{
printf("updating!\n");
sigTest();
}
Process.h:
class Process {
public:
void process();
};
Process.cpp:
void Process::process()
{
printf("hi!\n");
}
Glue.h:
class Glue {
public:
Glue();
void callUpdate();
private:
IVSig *sig;
Process *proc;
};
Glue.cpp:
Glue::Glue()
{
sig = new VSig();
proc = new Process();
sig->sigTest.connect(boost::bind(&Process::process, proc));
callUpdate();
}
void Glue::callUpdate()
{
sig->update();
}
This should print ‘updating!’ followed by ‘hi!’ but only outputs ‘updating!’.
Any help or suggestions are greatly appreciated!
The problem is because you are redeclaring sigTest in VSig class, and when you use the sig->sigTest you are accessing the sigTest member of the IVSig class ( the declared type ), but when the update is executed you are calling the virtual update method, that will use it’s local sigTest variable. Not sure if I was clear enough.
So, the rule of thumb is: never redefine a variable with the same name inside an inherited class.
Hope that helps!