ここでは、コードベースの例を示します。それは単純化されているので、私はそれがコンパイルされることを保証しませんが、それは近いはずです。 SublocationはあなたのクラスAであり、Slot1はあなたのクラスBです。私たちはこのようないくつかのスロットを持っています。それぞれが異なるシグナルのサブセットに加入しています。このスキームを使用する利点は、Sublocationがいずれのスロットについても何も知らず、スロットが継承階層の一部である必要がなく、気になるスロットの機能を実装するだけで済むという点です。これを使用して、非常にシンプルなインターフェイスでカスタム機能をシステムに追加します。
Sublocation.h
class Sublocation
{
public:
typedef boost::signal<void (Time, Time)> ContactSignal;
typedef boost::signal<void()> EndOfSimSignal;
void endOfSim();
void addPerson(Time t, Interactor::Ptr i);
Connection addSignalContact(const ContactSignal::slot_type& slot) const;
Connection addSignalEndOfSim(const EndOfSimSignal::slot_type& slot) const;
private:
mutable ContactSignal fSigContact;
mutable EndOfSimSignal fSigEndOfSim;
};
Sublocation.C
void Sublocation::endOfSim()
{
fSigEndOfSim();
}
Sublocation::Connection Sublocation::addSignalContact(const ContactSignal::slot_type& slot) const
{
return fSigContact.connect(slot);
}
Sublocation::Connection Sublocation::addSignalEndOfSim(const EndOfSimSignal::slot_type& slot) const
{
return fSigEndOfSim.connect(slot);
}
Sublocation::Sublocation()
{
Slot1* slot1 = new Slot1(*this);
Slot2* slot2 = new Slot2(*this);
}
void Sublocation::addPerson(Time t, Interactor::Ptr i)
{
// compute t1
fSigOnContact(t, t1);
// ...
}
Slot1.h
class Slot1
{
public:
Slot1(const Sublocation& subloc);
void onContact(Time t1, Time t2);
void onEndOfSim();
private:
const Sublocation& fSubloc;
};
Slot1.C
Slot1::Slot1(const Sublocation& subloc)
: fSubloc(subloc)
{
subloc.addSignalContact(boost::bind(&Slot1::onContact, this, _1, _2));
subloc.addSignalEndSim(boost::bind(&Slot1::onEndSim, this));
}
void Slot1::onEndOfSim()
{
// ...
}
void Slot1::onContact(Time lastUpdate, Time t)
{
// ...
}
関数をオーバーロードすることは可能ですか?もしそうなら、それを追加してもかまいません。 s.t. PrintNum(int)のようなものがあります。およびPrintNum(float)。 – pyInTheSky
@pyInTheSky関数型(厳密な用語は不明): '(void(*)(int))&PrintNum' – Qix