问题
When using Boost.Signals, boost allows you to derive from boost::signals::trackable
in order to ease object/connection lifetime management (See the boost documentation).
I am in an early stage of my project and I am thinking, whether to derive from boost::signals::trackable
in
- every new class I write that might use Boost.Signals in the future
- or only in classes I am sure that they will need the functionality of the
trackable
bas e-class
The main reason for th first approach would be to prevent me to forget deriving from boost::signals::trackable
.
Also double deriverations like
class Foo : public Base, public boost::signals::trackable
{
};
get unnecessary.
On the other side, preventing memory-leaks shouldn't be a main design-aspect. Testing and profiling tools like valgrind should be used to detect memory leaks.
Which approach is more suitable for growing projects?
回答1:
Note that Boost.Signals2
supersedes Boost.Signals
. It has much more flexible and powerful tracking mechanism.
Although the library aims to provide a thread-safe solution for multi-threaded programs, the locking overhead can be avoided in a single-threaded environment by setting boost::signals2::dummy_mutex
as signal's internal mutex.
回答2:
Qt as an alternative
The Qt-Event system enforces programmer to derive from QObject
so you really are on the save side when using Qt-Events.
来源:https://stackoverflow.com/questions/16845424/how-often-to-derive-from-boostsignalstrackable