Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling toLatin1().data() via a wrapper function c_str()
Forum Updated to NodeBB v4.3 + New Features

Calling toLatin1().data() via a wrapper function c_str()

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 534 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anestis_Papadopulos
    wrote on last edited by
    #1

    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()               : ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ: 
    =================================: 
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Anestis_Papadopulos
        wrote on last edited by
        #3

        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 :)

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          C++ basics - in your second example the temporary is deleted after the usage. Simply said: Temporaries are deleted when the ; is processed.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved