Why is my overloaded C++ constructor not called?

前端 未结 6 1751
暖寄归人
暖寄归人 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:09

    The type of "Just a test..." is const char *, which can be implicitly converted to either bool or std::string. Because std::string isn't a built in type, the const char *s is converted to a bool. You can prevent that by explicitly converting the const char * to a std::string:

    Test test(std::string("Just a test..."));
    

提交回复
热议问题