KodeWarrior KodeWarrior - 2 months ago 4x
C++ Question

Ambigious type conversion in constructor call

class String {
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 ?


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

You meant to write:

String mystring = "x";