为什么我的重载的C ++构造函数不被调用? [英] Why is my overloaded C++ constructor not called?

查看:257
本文介绍了为什么我的重载的C ++构造函数不被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的类:

I have a class like this one:

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

};

如果我创建一个这样的对象:

If I create an object like this:

Test test("Just a test...");

bool构造函数被调用!

The bool constructor is called!

任何人都知道为什么?

感谢

推荐答案

c $ c>只是一个测试...是 const char * ,可以隐式转换为 bool std :: string 。因为 std :: string 不是内置类型,所以 const char * s将转换为 bool 。您可以通过将 const char * 显式转换为 std :: string

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..."));

这篇关于为什么我的重载的C ++构造函数不被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆