I’m confused about following program about why it calls first constructor.
class A
{
public:
A(const char *c="\0")
{
cout<<"Constructor without arg";
}
A(string c)
{
cout<<"New one";
}
};
int main()
{
A a="AMD";
return 0;
}
Output is
Constructor without arg
"AMD"is aconst char[], which is implicitly converted toconst char*, so the first constructor [A(const char *c="\0")]is the best match.Note that
A(const char *c="\0")is not a constructor without an argument, it’s a constructor which takes a singleconst char*as an argument, and has an optional default value to use when aconst char*isn’t specified. In this case, you’re passing aconst char*, so it uses it.