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?

6
  • One question per question please. Commented Apr 5, 2013 at 13:58
  • 2
    "I can imagine that "3" will be converted to 3" -- you can imagine it, but you'd be wrong. Commented Apr 5, 2013 at 14:00
  • @RogerLipscombe: He meant that, C++ aside, there may be an intuitive way to convert such data at an abstract level. Commented Apr 5, 2013 at 14:01
  • @RogerLipscombe: (For example, both JavaScript and PHP are capable of making such a conversion, in a human-intuitive manner.) Commented Apr 5, 2013 at 14:09
  • @LightnessRacesinOrbit: Yes. I know. Commented Apr 5, 2013 at 14:54

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 When you say that 'x' is a number, you may want to mention what number it is (it's 120).
I learned integer, real and complex numbers. I also know quaternions but I never herd about character literal numbers... How do one work with them? What will be result of 'x' + 2?
@dasblinkenlight: Not necessarily! It may be; it may just as easily not be.
@Roman: Depends on the character set in use. For ASCII, x is 120, so the result of 'x'+2 is 122. Try it!
@Lightness Races in Orbit, does it mean that if I put explicit keyword in front of the first constructor and then try String mystring='x';, I will get an error message.
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.

Comments

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).

1 Comment

(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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.