1. <tfoot id='T8paR'></tfoot>

      1. <small id='T8paR'></small><noframes id='T8paR'>

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

      3. CString 到 char*

        CString to char*(CString 到 char*)
      4. <small id='ACVYp'></small><noframes id='ACVYp'>

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

              • <bdo id='ACVYp'></bdo><ul id='ACVYp'></ul>

                  <legend id='ACVYp'><style id='ACVYp'><dir id='ACVYp'><q id='ACVYp'></q></dir></style></legend>
                1. <tfoot id='ACVYp'></tfoot>

                    <tbody id='ACVYp'></tbody>

                2. 本文介绍了CString 到 char*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们在大部分代码中都使用 CString 类.但是有时我们需要转换为 char *.目前我们一直在使用 variable.GetBuffer(0) 来做这件事,这似乎有效(这主要发生在将 Csting 传递到函数需要 char * 的函数时).该函数接受这一点,我们继续前进.

                  然而,我们最近开始担心这是如何运作的,以及是否有更好的方法来做到这一点.

                  我理解它的工作方式是将一个字符指针传递给指向 CString 中第一个字符的函数,并且一切正常.

                  我想我们只是担心内存泄漏或任何不可预见的情况,这可能不是一个好主意.

                  解决方案

                  如果你的函数只需要读取字符串而不需要修改它,请将它们更改为接受 const char * 而不是 char*.CString 将自动为您转换,这是大多数 MFC 函数的工作方式,而且非常方便.(实际上 MFC 使用 LPCTSTR,它是 const TCHAR * 的同义词 - 适用于 MBC 和 Unicode 构建).

                  如果您需要修改字符串,GetBuffer(0) 非常危险 - 它不一定会为结果字符串分配足够的内存,并且您可能会遇到一些缓冲区溢出错误.>

                  正如其他人提到的,您需要在 GetBuffer 之后使用 ReleaseBuffer.您无需为转换为 const char * 执行此操作.

                  We are using the CString class throughout most of our code. However sometimes we need to convert to a char *. at the moment we have been doing this using variable.GetBuffer(0) and this seems to work ( this mainly happens when passing the Csting into a function where the function requires a char *). The function accepts this and we keep going.

                  However we have lately become worried about how this works, and whether there is a better way to do it.

                  The way i understand it to work is it passes a char pointer into the function that points at the first character in the CString and all works well.

                  I Guess we are just worried about memory leaks or any unforseen circumstances where this might not be a good idea.

                  解决方案

                  If your functions only require reading the string and not modifying it, change them to accept const char * instead of char *. The CString will automatically convert for you, this is how most of the MFC functions work and it's really handy. (Actually MFC uses LPCTSTR, which is a synonym for const TCHAR * - works for both MBC and Unicode builds).

                  If you need to modify the string, GetBuffer(0) is very dangerous - it won't necessarily allocate enough memory for the resulting string, and you could get some buffer overrun errors.

                  As has been mentioned by others, you need to use ReleaseBuffer after GetBuffer. You don't need to do that for the conversion to const char *.

                  这篇关于CString 到 char*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

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

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

                      1. <legend id='nKQOB'><style id='nKQOB'><dir id='nKQOB'><q id='nKQOB'></q></dir></style></legend>
                        • <tfoot id='nKQOB'></tfoot>