Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

std::wstring member function .c_str() help

This topic is 2705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im trying to understand




std::wstring string1("hello");

string1.c_str();




1.) wether this returns a 1 byte character string or a 2 byte character string.

2.) and also wether it automatically appends the '\0' character at the end of the returned string.





whenever i go to the functions definition in visual studio 2010....it takes me to the code below....




const _Elem *c_str() const
{ // return pointer to null-terminated nonmutable array
return (_Myptr());
}







i dont understand how this is the function and if i might be in the wrong spot..

Share this post


Link to post
Share on other sites
Advertisement
std::wstring::c_str() returns const wchar_t*. The size of wchar_t depends on the compiler. In GCC it is 4, I think it is 2 in Visual C++. And yes the it is null terminated ('\0').

The reason why the function looks so simple is that it only have to return a pointer to it's internal buffer. No conversions necessary.

Share this post


Link to post
Share on other sites
std::wstring is std::basic_string<wchar_t>, so it will use wchar_t sized characters. wchar_t can be bigger than 2 bytes.
std::basic_string<T>::c_str() appends the NUL character, as indicated in the comments of the function.



[color=#1C2837][size=2]i dont understand how this is the function and if i might be in the wrong spot..[/quote]
[color=#1C2837][size=2]Reading the internals of the standard library implementation is not for the faint of heart. But yes, you're in the right spot.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!