Sign in to follow this  
nuclear123

std::wstring member function .c_str() help

Recommended Posts

im trying to understand




std::wstring string1("hello");

string1.c_str();




1.) wether this returns a [b]1 byte character string[/b] or a [b]2 byte character string.[/b]

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




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




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





[/code]

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
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.


[quote]
[color=#1C2837][size=2]i dont understand how this is the function and if i might be in the wrong spot..[/quote][/size][/color]
[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.[/size][/color]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this