Re: Mutual dependence
On Sep 20, 7:08 am, lukasz....@gmail.com wrote:
I have two classes representing master and slave. Both come in several
variants:
MasterUnsafe, MasterThreadSafe, ...
SlaveLogging, SlaveIgnore, ...
Now It turns out that they need to call each other's methods. And I
can't afford to have virtualcall
especially when slave is SlaveIgnore and does nothing.
I need to have objects of types of any possible combination like:
MasterUnsafe+SlaveLogging
MasterUnsafe+SlaveIgnore
MasterThreadSafe+SlaveLogging
MasterThreadSafe+SlaveIgnore
I thought that inheritnce can help but I can't get it to work.
I guess I have much to learn :)
Can someone give me some hints?
You could use policy based design, like in http://en.wikipedia.org/wiki/Policy-based_design
Something along these lines:
template<class SafeUnsafe, class LoggingIgnore>
struct master;
template<class SafeUnsafe, class LoggingIgnore>
struct slave
{
typedef master<SafeUnsafe, LoggingIgnore> master;
void just_do_it(master* m) { m->do_it_yourself(); }
void no_you_do_it(master* m) { this->all_right_then(m); }
void all_right_then(master*);
};
template<class SafeUnsafe, class LoggingIgnore>
struct master
{
typedef slave<SafeUnsafe, LoggingIgnore> slave;
slave slave_;
void do_it() { slave_.just_do_it(this); }
void do_it_yourself() { slave_.no_you_do_it(this); }
};
int main()
{
master<int, int> m;
m.do_it();
}
--
Max
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]