Why is my overloaded C++ constructor not called?

前端 未结 6 1649
暖寄归人
暖寄归人 2021-02-20 12:17

I have a class like this one:

class Test{
public:
  Test(string value);
  Test(bool value);

};

If I create an object like this:



        
6条回答
  •  花落未央
    2021-02-20 13:15

    The type of "Just a test..." is const char*. There is a built-in conversion from pointers to bool which is preferred over the non-built-in conversion from const char* to std::string.

    The reason that the bool conversion is preferred is because std::string, while part of the standard library, is not a built-in type like integers, pointers, and booleans. It acts like any other class, and so its conversion constructors are considered only after conversions to built-in types.

提交回复
热议问题