小编use*_*138的帖子

C++错误的构造函数被调用

我希望下面的代码可以打印Test::Test(string,string,bool),但是会打印出来Test::Test(string,bool).为什么在提供两个字符串时只调用一个字符串参数的构造函数?当然一个字符串不能转换为bool ......?我尝试添加显式关键字,但它没有帮助.代码也在http://ideone.com/n3tep1.

#include <iostream>
#include <string>

using namespace std;

class Test
{
public:

    Test(const string& str1, bool flag=false)
    {
        cout << "Test::Test(string,bool)" << endl;
    }

    Test(const string& str1, const string& str2, bool flag=false)
    {
        cout << "Test::Test(string,string,bool)" << endl;
    }
};

int main()
{
    Test* test = new Test("foo", "bar");
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

9
推荐指数
2
解决办法
1238
查看次数

标签 统计

c++ ×1

c++11 ×1