[Interest] Converting std::string.c_str() code to use QString methods

Konstantin Tokarev annulen at yandex.ru
Sun Sep 1 15:26:11 CEST 2013



30.08.2013, 23:09, "Michael Jackson" <imikejackson at gmail.com>:
> I have a large code base that we are migrating to more fully utilize Qt classes. Many places in the code have lines like the following:
>
> std::string path("/path/to/foo.txt");
> FILE* f = fopen(path.c_str(), "wb");
>
> If the "path" variable is now declared as a QString which QString method would be me the equivalent as the "c_str()" of std::string?
>
> Not sure if I should be using "toASCII()" or "data()" or "toUtf8()" or what exactly.

Note that if you replace std::string with QString, EACH TIME you use it in C string context you will trigger conversion from UTF16 to UTF8, because QString stores its data in UTF16. It also takes 2x more memory for ASCII characters which can matter in some cases.

QByteArray is a more straightforward replacement for std::string, it does not introduce conversion and memory overheads while providing reach API and platform independent copy on write behavior.

>
> Google didn't really give me a definitive answer.
>
> Many Thanks
> --
> Mike Jackson
> imikejackson & gmail * com
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

-- 
Regards,
Konstantin



More information about the Interest mailing list