3

Suppose we have a class String:

class String {
   public: 
   String (int n);
   String(const char *p);
}

What will happen if we try:

String mystring='x';

Here it is written that the char 'x' will be converted to int and will call String(int) constructor. However, I do not understand it.

First, how 'x' can be converted to int? I can imagine that "3" will be converted to 3 but what "x" will be converted to? Second, we have two constructors in the class. The first constructor takes one argument of int type and another constructor takes a pointer to char variable as an argument. Now we try to call not existing constructor that takes char as an argument. So, we convert char to integer, but why we do not try to convert char to pointer to char and then use the second constructor?

Community
  • 1
  • 1
Roman
  • 124,451
  • 167
  • 349
  • 456

3 Answers3

9

First, how 'x' can be converted to int? I can imagine that "3" will be converted to 3 but what "x" will be converted to?

Don't make the mistake of confusing 'x' and "x".

  • "x" is a string literal and, you're right, it would be meaningless to try to convert this to int;
  • 'x' is a character literal, which is already basically a number (equal to the underlying value of whatever the character x maps to in your basic source character set; for ASCII, this is 120); converting this to integer is trivial.

So, we convert char to integer, but why we do not try to convert char to pointer to char and then use the second constructor?

There are rules in C++ that govern which is the "best match". In this case, it's the int overload. In the case that both are an equal match, your compiler will present an "ambiguous overload" error and you will be forced to convert 'x' explicitly (i.e. manually, with a cast) in order to dictate which you wanted.

In your case this is not a problem, since non-pointers actually cannot convert to pointers.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
4

char is an integral type and any value of char can be represented by a larger integral type such as int or unsigned int. '3' is not converted to 3 either, usually. In ASCII the character '3' has the value 48 and so on any system that uses ASCII (most all of them) that is the value you get.

The pointer constructor is not used because C++ does not allow implicit conversion from char to pointer types. The int constructor is the only viable option and so it is selected.

bames53
  • 86,085
  • 15
  • 179
  • 244
1

The character x will be converted to the integer 120, as you can see here. Also, recall that there is no conversion from types to pointers, or conversion between pointers of one type to pointer to another type, and this is obvious if you think how bout pointer arithmetic works.

You cannot convert to type to pointer because this requires allocating memory, and you have to do this explicit to the compiler (it will not do it automatically) .

So, basically the compiler is converting a char to an int because is the only think it can do. The compiler should complain if the function call is ambiguous (but it might depend on the compiler, I am not sure).

FKaria
  • 1,012
  • 13
  • 14
  • (1) ASCII is not guaranteed. | (2) `You cannot convert to type to pointer because this requires allocating memory` Nonsense; creating a pointer does not require creating a pointee. – Lightness Races in Orbit Apr 05 '13 at 14:10