C++ — Why should we use explicit in this constructor?

前端 未结 2 511
暖寄归人
暖寄归人 2021-01-21 07:22

Please refer to Wikipedia:Strategy Pattern (C++)

class Context
{
    private:
        StrategyInterface * strategy_;

    public:
        explicit Context(Strate         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 07:28

    Because it's generally a good idea to use explicit unless you really want to allow implicit conversion. Since you're unlikely to use a Context object in a situation where you really gain anything from an implicit conversion, you're better off making it explicit.

提交回复
热议问题