The return value of cstring. getlength is 0.

Source: Internet
Author: User

Debug todayProgramThe value cstring. getlength In the CS ring object is 0,CodeAs follows:

Cstring m_sername;

Cstring S = "hello"

M_sername = s;

S. getlength () = 0;

Find the cause on the Internet in str1.releasebuffer (); in fact, cstring: releasebuffer is just an image, and it is the name corresponding to getbuffer.

Let's take a look at the releasebuffer code in MFC:

Void releasebuffer (INT nnewlength =-1)

{

If (nnewlength =-1)

{

Nnewlength = stringlength (m_pszdata );

}

Setlength (nnewlength );

}

Obviously, releasebuffer only plays a role in updating the length of a string. In cstring, getlength does not calculate the string length dynamically, but is calculated and saved in an int variable after the value assignment operation. When cstring is directly modified through getbuffer, the Int variable cannot be updated automatically, so the releasebuffer is available.

Therefore, the above Code adds S. releasebuffer (); and then S. getlength () is OK.

Please refer to the following link for more information: http://iqnix.blog.163.com/blog/static/217219820098441153853/. thank you !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.