简体   繁体   中英

String implicit conversion operators to const char*/wchar_t *

I find MFC/ATL CString class very convenient in Win32 C++ code; in particular I found convenient the fact that we can pass instances of CString to Win32 API's LPCWSTR (ie const wchar_t * ) parameters directly, thanks to implicit conversion operator defined by CString .

Instead, when using std::wstring , an explicit call to .c_str() method is required.

So, why do the STL string classes (both std::string and std::wstring ) require an explicit method call ( c_str() ) instead of defining an implicit const char* / const wchar_t * conversion operator?

Are there serious pitfalls hidden with implicit conversion operators?

Whatever the theoretical concerns are, I will only note that in years of working with CString I've never been bitten by this feature. The convenience far outweighs the dangers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM