C++ question

I came across an interesting problem yesterday (for some value of interesting, anyway). Basically, I’m trying to figure out a way of allowing a method in a base class to access a method in the derived class (assuming this method is not declared in the base class). In other words, something like:

class Base
{
public:
//Blah
void myMethod() { derivedMethod(); }
};

class Derived : public Base
{
public:
void derivedMethod () {}
}

Okay, normally you’d use a virtual base method for this and it’d all be hunkydory. But in this instance I don’t want to do that – I’m working on some policy based programming, so I’ve got this situation:

class PolicyA
{
public:
void policyImplementor() { hostMethod(); /* other stuff */ }
};

class PolicyB
{
public:
void policyImplementor() { hostMethod(); /* other different stuff */ }
};

template < class Policy >
class PolicyHost : public Policy
{
public:
void hostMethod() { /* Do something */ }
}

Except that doesn’t work. Making a common base class for PolicyA and PolicyB which contains a virtual hostMethod() is one way to fix it, but I’m sure there must be another way. I’ve tried declaring the policy classes to be templated thus:

template < class Deriver >
class PolicyA : public Deriver { /* etc */ };

template < class < class > Policy>
class PolicyHost: public Policy<PolicyHost> { /* etc */ };

But that doesn’t work either. Anyone got any suggestions (other than “Design your classes so this isn’t necessary”) ?

One Response to “C++ question”

  1. SharkyUK says:

    Interesting… I’d also like to know how this could be done, out of curiosity more than anything.