<tfoot id='Dy15T'></tfoot>

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

      1. <legend id='Dy15T'><style id='Dy15T'><dir id='Dy15T'><q id='Dy15T'></q></dir></style></legend>
      2. <small id='Dy15T'></small><noframes id='Dy15T'>

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

        std::string::c_str() 和临时变量

        std::string::c_str() and temporaries(std::string::c_str() 和临时变量)

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

                  本文介绍了std::string::c_str() 和临时变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下 C++ 代码格式是否正确:

                  Is the following C++ code well-formed:

                  void consumer(char const* p)
                  {
                    std::printf("%s", p);
                  }
                  
                  std::string random_string_generator()
                  {
                    // returns a random std::string object
                  }
                  
                  consumer(random_string_generator().c_str());
                  

                  我遇到的问题是,在创建临时 std::string 对象并获取 c_str() 指针后,没有什么可以阻止 std::string 对象被破坏(或者我可能错了?).如果代码没问题的话,你能不能给我指出标准.当我用 g++ 测试时,它确实有效.

                  The problem I have with it is, that after creating the temporary std::string object and taking the c_str() pointer, nothing prevents the std::string object from getting destroyed (or maybe I'm wrong?). Can you please point me to the standard, if the code is OK despite everything. It does work, when I test with g++.

                  推荐答案

                  std::string::c_str()返回的指针指向内存由字符串对象维护.它一直有效,直到非常量在字符串对象上调用函数,或者字符串对象是毁坏了.您关心的字符串对象是临时对象.它将在完整表达式的末尾销毁,而不是在之前和不是之后.在您的情况下,完整表达式的结尾在调用 consumer,所以你的代码是安全的.如果 consumer将指针保存在某处,以备后用.

                  The pointer returned by std::string::c_str() points to memory maintained by the string object. It remains valid until a non-const function is called on the string object, or the string object is destructed. The string object you're concerned about is a temporary. It will be destructed at the end of the full expression, not before and not after. In your case, the end of the full expression is after the call to consumer, so your code is safe. It wouldn't be if consumer saved the pointer somewhere, with the idea of using it later.

                  从 C++98 开始就严格定义了临时对象的生命周期.在此之前,它会有所不同,具体取决于编译器和您编写的代码编写的不会与 g++ 一起工作(1995 年之前,粗略地—g++当标准委员会投票时几乎立即改变了这一点).(那时也没有 std::string,但同样的问题会影响任何用户编写的字符串类.)

                  The lifetime of temporaries has been strictly defined since C++98. Before that, it varied, depending on the compiler, and the code you've written wouldn't have worked with g++ (pre 1995, roughly—g++ changed this almost immediately when the standards committee voted it). (There wasn't an std::string then either, but the same issues affect any user written string class.)

                  这篇关于std::string::c_str() 和临时变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与自定义类对象的向量一起使用?)
                    <tbody id='gLUYM'></tbody>
                  • <i id='gLUYM'><tr id='gLUYM'><dt id='gLUYM'><q id='gLUYM'><span id='gLUYM'><b id='gLUYM'><form id='gLUYM'><ins id='gLUYM'></ins><ul id='gLUYM'></ul><sub id='gLUYM'></sub></form><legend id='gLUYM'></legend><bdo id='gLUYM'><pre id='gLUYM'><center id='gLUYM'></center></pre></bdo></b><th id='gLUYM'></th></span></q></dt></tr></i><div id='gLUYM'><tfoot id='gLUYM'></tfoot><dl id='gLUYM'><fieldset id='gLUYM'></fieldset></dl></div>

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

                        <tfoot id='gLUYM'></tfoot>
                          <bdo id='gLUYM'></bdo><ul id='gLUYM'></ul>
                          1. <small id='gLUYM'></small><noframes id='gLUYM'>