KodeWarrior KodeWarrior - 1 year ago 90
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 ?

Answer Source

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

You meant to write:

String mystring = "x";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download