Re: How to initialise member variable in template construction
Angus <anguscomber@gmail.com>, on 01/09/2010 14:43:29, wrote:
I am trying to set the strategy (algorithm) used in a context by
template. Here is my context class (which is incorrectly
implemented):
template<class TStrategy>
class Context //TStrategy is the algorithm
{
private:
TStrategy * strategy_; //knows about StrategyInterface
public:
void execute() const
{
strategy_->execute();
}
};
The strategies:
class StrategyInterface
{
public:
virtual void execute() const = 0; //abstract class - interface
only
};
//the actual concrete algorithms
class ConcreteStrategyA: public StrategyInterface
{
public:
virtual void execute() const
{
cout<< "Called ConcreteStrategyA execute method"<< endl;
}
};
And I want to do something like this:
Context<ConcreteStrategyA> contextD;
contextD.execute();
But I need to initialise the context data member properly. how do I
do that?
I'm not sure about "how" you would do that, but I know "where" you
should do that: in a constructor. There is none in the code you posted, why?
--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com