KodeWarrior KodeWarrior - 3 months ago 8
C++ Question

Ambigious type conversion in constructor call

class String {
public:
String(int n); // allocate n bytes to the String object
String(const char *p); // initializes object with char *p
};


Now the below code

String mystring = 'x';


This calls the String( int n ) and not String( const char* ).
Why is there a type conversion happening even when I pass a const char* ? I know a explicit solves this issue but why is the type conversion happening in the first place even when I pass the correct type ?

Answer

You are not passing a const char *. You are passing a char.

You meant to write:

String mystring = "x";
Comments