Re: How to initialise member variable in template construction
Angus <anguscomber@gmail.com>, on 01/09/2010 15:28:18, wrote:
On 1 Sep, 23:11, red floyd<redfl...@gmail.com> wrote:
On Sep 1, 2:55 pm, Angus<anguscom...@gmail.com> wrote:
On 1 Sep, 22:51, "Francesco S. Carta"<entul...@gmail.com> wrote:
Angus<anguscom...@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-Hidequoted text -
- Show quoted text -
I guess I just need to add this:
Context(){ strategy_ = new TStrategy; }
~Context() { delete strategy_; }
Could I do it without using the heap?
Not if you store a pointer, You defined stategy_ as a TStrategy*.
You could go with a straight-up strategy member.
template<class TStrategy>
class Context //TStrategy is the algorithm
{
private:
TStrategy strategy_; //knows about StrategyInterface
public:
void execute() const
{
strategy.execute();
}
};
Then there's no allocation needed, and strategy_ is automatically
constructed.- Hide quoted text -
- Show quoted text -
That's interesting I tried that:
template<class TStrategy>
class Context //TStrategy is the algorithm
{
private:
TStrategy strategy_; //knows about StrategyInterface
public:
void execute() const
{
strategy_.execute();
}
};
And it works without my defining a constructor. I assume the compiler
is constructing strategy_ somehow behind the scenes. Could I create
my own constructor explicitly? What would it look like?
Do you mean a constructor for Context? If you have nothing in particular
to do, you can omit it as shown in the code above, as the compiler will
generate the trivial constructor for you.
The above works because your TStrategy is a type that can be
default-initialized. Assume that TStrategy cannot be
default-initialized, e.g.
class TStrategy {
public:
TStrategy(int i) : data(i) {}
void execute() const {
// use data
}
private:
int data;
};
Assuming the above, you cannot create an instance with "TStrategy ts;",
but you need something like "TStrategy ts(42);", but you cannot when
declaring normal data members (because they cannot be initialized in
their declarations), so, in your Context class, you'd need a constructor
like the one I posted elsethread:
Context() : strategy_(42) {}
Nothing more than that, in a simple case like the one I depicted.
Though, in your case, that seems to be unneeded.
--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com