Policy Based Design Question

D

DerrickH

I have a template class with three policies: PolicyA, PolicyB, and
PolicyC. The design dilemma I am having is that B and C depend upon A,
but I would like my Host class to derive from all three. In all three
cases I would like to take advantage of enriched policies.

template <
class PolicyA,
class Host: public PolicyA, public PolicyB<PolicyA>, public
PolicyC<PolicyA>
{
};

How do I structure the policy classes A, B, and C such that B and C
have access to A (Host)?
 
D

DerrickH

The question is a bit vague.  Do you need the instances of the base
classes B and C (base class subobjects of 'Host') to have access to the
instance of the base class A (the subobject of the same 'Host')?  In
that case you need to construct PolicyB and PolicyC with, say, a pointer
to 'PolicyA' object.  So, when you construct the Host, you'd do
something like this

     Host()
         : PolicyA()
         , PolicyB<PolicyA>(this)
         , PolicyC<PolicyA>(this)
     {
     }

Provided that 'PolicyB' and 'PolicyC' can be constructed from a pointer
to 'PolicyA', 'this' expression will be converted to 'PolicyA*', and the
other two subobjects will receive the correct address of 'PolicyA'.

If that's not what you're asking, then please show us what kind of
"access" you expect (better in a C++ form) so it would be possible to
understand what you're trying to accomplish.

V

Thank you, Victor. Sorry for being vague. That is exactly what I'm
looking for. And, actually after giving it some thought, C also
depends upon B. Let me make it a little more clear if I can. Basically
I'm trying to model a service provider which provides two different
services.

template <
class ConnectionDetails,
class service_provider :
public ConnectionDetails,
public Service1<ConnectionDetails>,
public Service2<ConnectionDetails,
Service1<ConnectionDetails> >
{
public:
service_provider() :
ConnectionDetails(),
Service1<ConnectionDetails>(this),
Service2<ConnectionDetails,
Service1<ConnectionDetails> >(this, this)
{
}
};

The solution you provided is what I'm looking for, but is there a way
to avoid the this->copy_of_this-> extra level of indirection that
would result? Or, even better, is there a better way to model this
type of relationship?

Thanks again.

-Derrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top