Why does C++ allow an integer to be assigned to a string? -


I had an interesting situation today in such a program where I unknowingly provided an unsigned integer for a std :: string Specified. The VisualStudio C ++ compiler did not make any warnings or errors about it, but I noted the bug when I started the project and it gave me junk characters for my string.

This is a code like this:

  std :: string my_string (""); Unsigned int my_number = 1234; My_string = my_number;  

The following code also compiles exactly:

  std :: string my_string (""); Unsigned int my_number = 1234; My_string.operator = (my_number);  

The following results in an error:

  unsigned int my_number = 1234; Std :: string my_string (my_number);  

What's going on? How the compiler will stop the build with the previous code block, but let's create the first 2 code blocks?

Because the string is assigned from char , and Int is variable over variable on char .


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -