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

前端 未结 4 2156
长情又很酷
长情又很酷 2020-11-27 06:32

I encountered an interesting situation today in a program where I inadvertantly assigned an unsigned integer to a std::string. The VisualStudio C++ compiler did not give an

4条回答
  •  自闭症患者
    2020-11-27 07:08

    It is definitely operator=(char ch) call - my debugger stepped into that. And my MS VS 2005 compiles following without error.

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

提交回复
热议问题