<bdo id='kTo4X'></bdo><ul id='kTo4X'></ul>

  1. <tfoot id='kTo4X'></tfoot>
    <i id='kTo4X'><tr id='kTo4X'><dt id='kTo4X'><q id='kTo4X'><span id='kTo4X'><b id='kTo4X'><form id='kTo4X'><ins id='kTo4X'></ins><ul id='kTo4X'></ul><sub id='kTo4X'></sub></form><legend id='kTo4X'></legend><bdo id='kTo4X'><pre id='kTo4X'><center id='kTo4X'></center></pre></bdo></b><th id='kTo4X'></th></span></q></dt></tr></i><div id='kTo4X'><tfoot id='kTo4X'></tfoot><dl id='kTo4X'><fieldset id='kTo4X'></fieldset></dl></div>

      <small id='kTo4X'></small><noframes id='kTo4X'>

      <legend id='kTo4X'><style id='kTo4X'><dir id='kTo4X'><q id='kTo4X'></q></dir></style></legend>
    1. 为什么 C++ 允许将整数分配给字符串?

      Why does C++ allow an integer to be assigned to a string?(为什么 C++ 允许将整数分配给字符串?)
        <bdo id='NMa5n'></bdo><ul id='NMa5n'></ul>

            <tbody id='NMa5n'></tbody>

              <i id='NMa5n'><tr id='NMa5n'><dt id='NMa5n'><q id='NMa5n'><span id='NMa5n'><b id='NMa5n'><form id='NMa5n'><ins id='NMa5n'></ins><ul id='NMa5n'></ul><sub id='NMa5n'></sub></form><legend id='NMa5n'></legend><bdo id='NMa5n'><pre id='NMa5n'><center id='NMa5n'></center></pre></bdo></b><th id='NMa5n'></th></span></q></dt></tr></i><div id='NMa5n'><tfoot id='NMa5n'></tfoot><dl id='NMa5n'><fieldset id='NMa5n'></fieldset></dl></div>
              • <small id='NMa5n'></small><noframes id='NMa5n'>

                <legend id='NMa5n'><style id='NMa5n'><dir id='NMa5n'><q id='NMa5n'></q></dir></style></legend>
                <tfoot id='NMa5n'></tfoot>
              • 本文介绍了为什么 C++ 允许将整数分配给字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我今天在一个程序中遇到了一个有趣的情况,我无意中将一个无符号整数分配给了一个 std::string.VisualStudio C++ 编译器没有给出任何警告或错误,但我在运行项目时碰巧注意到了这个错误,它为我的字符串提供了垃圾字符.

                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 any warnings or errors about it, but I happened to notice the bug when I ran the project and it gave me junk characters for my string.

                代码大概是这样的:

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

                以下代码也可以很好地编译:

                The following code also compiles fine:

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

                以下导致错误:

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

                这是怎么回事?为什么编译器会用最后一个代码块停止构建,而让前 2 个代码块构建?

                What is going on? How come the compiler will stop the build with the last code block, but let the first 2 code blocks build?

                推荐答案

                因为 string 可以从 char 赋值,而 int 可以隐式转换为 char.

                Because string is assignable from char, and int is implicitly convertible to char.

                这篇关于为什么 C++ 允许将整数分配给字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                • <i id='rTYZD'><tr id='rTYZD'><dt id='rTYZD'><q id='rTYZD'><span id='rTYZD'><b id='rTYZD'><form id='rTYZD'><ins id='rTYZD'></ins><ul id='rTYZD'></ul><sub id='rTYZD'></sub></form><legend id='rTYZD'></legend><bdo id='rTYZD'><pre id='rTYZD'><center id='rTYZD'></center></pre></bdo></b><th id='rTYZD'></th></span></q></dt></tr></i><div id='rTYZD'><tfoot id='rTYZD'></tfoot><dl id='rTYZD'><fieldset id='rTYZD'></fieldset></dl></div>

                  <small id='rTYZD'></small><noframes id='rTYZD'>

                • <tfoot id='rTYZD'></tfoot>
                    <tbody id='rTYZD'></tbody>
                    <bdo id='rTYZD'></bdo><ul id='rTYZD'></ul>

                          <legend id='rTYZD'><style id='rTYZD'><dir id='rTYZD'><q id='rTYZD'></q></dir></style></legend>