Calling toLatin1().data() via a wrapper function c_str()
-
Hi guys....
This is probably very basic but I can't figure it out atm. I need to have the char* data of my QString object returned and I shall use an "alias" function named "c_str()" for that. My class which derives from QString is called AquaString. When I call "obj.toLatin1().data();" (with "AquaString obj;") then the result is there. But when I call it via the alias function "obj.c_str();" then the result is broken. It has to do with the return of a temporary object or so. My question is, how do I do it right?
Here are the files to test this:
aquastring.hpp:#ifndef AQUASTRING_HPP #define AQUASTRING_HPP #include <QString> class AquaString : public QString { public: AquaString(const AquaString& other); AquaString(const QString& str); char* c_str(); const char* c_str() const; }; #endif // AQUASTRING_HPP
aquastring.cpp:
#include "aquastring.hpp" #include <QDebug> AquaString::AquaString(const AquaString& other) { if (*this != other) { *this = other; } } AquaString::AquaString(const QString& str) : QString(str) { // uses the QString constructor. } char* AquaString::c_str() { return toLatin1().data(); } const char* AquaString::c_str() const { return toLatin1().constData(); } // EOF
main.cpp:
#include <QDebug> #include "aquastring.hpp" int main(int argc, char **argv) { Q_UNUSED(argc); Q_UNUSED(argv); const QString tmp("From QString"); AquaString str(tmp); qDebug() << "=================================:"; qDebug() << "QString :" << tmp; qDebug() << "QString.toLatin1() :" << tmp.toLatin1(); qDebug() << "QString.toLatin1().data() :" << tmp.toLatin1().data(); qDebug() << "QString.toLatin1().constData() :" << tmp.toLatin1().constData(); qDebug() << "=================================:"; qDebug() << "AquaString :" << str; qDebug() << "AquaString.toLatin1() :" << str.toLatin1(); qDebug() << "AquaString.toLatin1().data() :" << str.toLatin1().data(); qDebug() << "AquaString.toLatin1().constData():" << str.toLatin1().constData(); qDebug() << "AquaString.c_str() :" << str.c_str(); return 0; }
and the result looks like:
=================================: QString : "From QString" QString.toLatin1() : "From QString" QString.toLatin1().data() : From QString QString.toLatin1().constData() : From QString =================================: AquaString : "From QString" AquaString.toLatin1() : "From QString" AquaString.toLatin1().data() : From QString AquaString.toLatin1().constData(): From QString AquaString.c_str() : ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ: =================================:
-
@Anestis_Papadopulos said in Calling toLatin1().data() via a wrapper function c_str():
char* AquaString::c_str()
{
return toLatin1().data();
}You create a temporary and return a dangling pointer.
-
Hi Christian. Thx for the quick answer.
I have a working version of the c_str() function and it looks like:
char* AquaString::c_str() { unsigned int len = length()+1; char* ret = new char[len]; memcpy(ret, toLatin1().data(), len); ret[len] = '\0'; return ret; }
The point is that I would rather call the QString functions somehow "direkt" instead of making copies via memcpy or similar functions. Is there a way to do that or am I just losing time here? I think the later is the case but maybe there is some kind of trick/call that I am not aware of... Thx for your time anyway :)
-
C++ basics - in your second example the temporary is deleted after the usage. Simply said: Temporaries are deleted when the
;
is processed.