請參考Wikipedia:Strategy Pattern (C++)C++ - 爲什麼我們應該在這個構造函數中使用explicit?
class Context
{
private:
StrategyInterface * strategy_;
public:
explicit Context(StrategyInterface *strategy):strategy_(strategy)
{
}
void set_strategy(StrategyInterface *strategy)
{
strategy_ = strategy;
}
void execute() const
{
strategy_->execute();
}
};
爲什麼它是一個很好的做法,使用顯式上下文的構造?
謝謝
如果不使用'explicit'的好地方,又是什麼? – Potatoswatter 2010-11-08 05:23:23
@Patatoswatter:任何地方你都有一個班級的名字不能在有禮貌的公司中大聲說出。 – 2010-11-08 05:41:36
詹姆斯的上述評論不被重視;只是爲了天真的初學者 – Jaywalker 2010-11-08 06:20:36