请参阅维基百科:策略模式(C++)
class Context
{
private:
StrategyInterface * strategy_;
public:
explicit Context(StrategyInterface *strategy):strategy_(strategy)
{
}
void set_strategy(StrategyInterface *strategy)
{
strategy_ = strategy;
}
void execute() const
{
strategy_->execute();
}
};
为什么对 Context 的构造函数使用显式是一个好习惯?
谢谢
因为通常使用它是一个好主意explicit
除非你really想要允许隐式转换。因为你不太可能使用Context
对象在你确实从隐式转换中获得任何东西的情况下,你最好这样做explicit
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)