In my private project I use boost::signals2 to send messages from several classes to a common receiver. My Question is: how can I send messages from the constructor of a class? The problem is, that the signal in this case does not exist until an object of class A is instantiated (until the constructor is executed). But at this point there was not yet a chance for some other code to establish a slot.
I tried to use static boost::signals2::signal<void(const std::string & Msg)> but then I get loader errors 'undefined reference to class::signal'.
Below is a very stripped down version of my code for illustration purposes. The question is: how can class B receive a message from the constructor of class A? Or with other words: how can I connect a slot to the signal of class A before the constructor of class A is executed?
class A {
public:
/*static*/ boost::signals2::signal<void(const std::string & Msg)> sigA;
A() { sigA("message from constructor A"); }
};
class B {
B() {
A a;
a.sigA.connect([](const std::string & msg) { std::cerr << msg << '\n'; } );
}
};
For your information: I use fedora 33, gcc 11.2.1 and C++17.
Please excuse my english: it's not my native language und i am missing some experience...