I have two constructors overloaded like this, the problem is that the wrong constructor overload is entered :
class Param
{
Param(
const std::string& name,
const MyVariant& variant = MyVariant()
const std::string& desc = std::string(""),
bool b1 = true,
bool b2 = true )
:
mName(name),
mValue(variant),
mDesc(desc),
mB1(b1),
mB2(b2) {}
Param(
const std::string& name,
const MyVariant& variant,
bool b1)
:
mName(name),
mValue(variant)
mB1(b1) {}
private:
std::string mName;
MyVariant mValue;
std::string mDesc;
bool mB1;
bool mB2;
};
Param p("name",4,"desc"); // this enters the second constructor, not the first why ?
It uses the second constructor because
"desc"is aconst char*that can be converted to aboolimplicitly, so that’s what the compiler does. There is no implicit conversion fromconst char*tostd::string, so the first constructor is not even a candidate here.To use the first constructor you ‘d need to explicitly pass in a
string: