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. QString to QString assignment
Forum Updated to NodeBB v4.3 + New Features

QString to QString assignment

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 192 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.
  • B Offline
    B Offline
    Bikash_R_Das
    wrote last edited by
    #1

    Hi,
    I m using Qt 6.5.7 for Embedded application.

    I have some query related to QString to QString assignment.

    I have a structure containg an enumeration data type and two QString s. Something like this:

    typedef struct Student {
        GENDER_TYPE    eGender; // possible values GENDER_TYPE_MALE, GENDER_TYPE_FEMALE and GENDER_TYPE_MAX 
        QString strName;
        QString strGroupName;
        Student()
        {
            eGender = GENDER_TYPE_MAX;
            strName = "";
            strGroupName = "";
        }
    } Student;
    

    Now say somewhere I have following code:

    Student stu1;
    stu1.eGender = GENDER_TYPE_MALE;
    stu1.strName = "Amit";
    stu1.strGroupName = "DOLPHIN";
    .
    .
    Student stu2;
    stu2 = stu1;
    

    Is it safe to do stu2 = stu1? Or do I need to have an assignment operator overloading for the structure?
    This doubt is related to QString assignment. As per my understanding, it is safe. But any input on the above will be a great help.

    Thanks in advance.

    JonBJ 1 Reply Last reply
    0
    • B Bikash_R_Das

      Hi,
      I m using Qt 6.5.7 for Embedded application.

      I have some query related to QString to QString assignment.

      I have a structure containg an enumeration data type and two QString s. Something like this:

      typedef struct Student {
          GENDER_TYPE    eGender; // possible values GENDER_TYPE_MALE, GENDER_TYPE_FEMALE and GENDER_TYPE_MAX 
          QString strName;
          QString strGroupName;
          Student()
          {
              eGender = GENDER_TYPE_MAX;
              strName = "";
              strGroupName = "";
          }
      } Student;
      

      Now say somewhere I have following code:

      Student stu1;
      stu1.eGender = GENDER_TYPE_MALE;
      stu1.strName = "Amit";
      stu1.strGroupName = "DOLPHIN";
      .
      .
      Student stu2;
      stu2 = stu1;
      

      Is it safe to do stu2 = stu1? Or do I need to have an assignment operator overloading for the structure?
      This doubt is related to QString assignment. As per my understanding, it is safe. But any input on the above will be a great help.

      Thanks in advance.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by JonB
      #3

      @Bikash_R_Das
      Yes you can just assign one struct from the other. As @Ben-Campbell-Wallis writes, QString does shared usage and only detaches/creates an actual copy if you change one of the QStrings. However that has nothing to do with the safety of the struct copy, and it would still be "safe" even if QString did not have this behaviour. This is down to the implementation of QString &QString::operator=(const QString &other) / QString::QString(const QString &other), and the fact that it is inside a whole struct being assigned is not important.

      1 Reply Last reply
      3
      • B Offline
        B Offline
        Ben Campbell-Wallis
        wrote last edited by Ben Campbell-Wallis
        #2

        @Bikash_R_Das I believe QString deep-copy behaviour is implicit (copy-on-write). There's no need for operator overloading.

        If in doubt here is the Qt doc:

        https://doc.qt.io/qt-6/qstring.html

        1 Reply Last reply
        3
        • B Bikash_R_Das

          Hi,
          I m using Qt 6.5.7 for Embedded application.

          I have some query related to QString to QString assignment.

          I have a structure containg an enumeration data type and two QString s. Something like this:

          typedef struct Student {
              GENDER_TYPE    eGender; // possible values GENDER_TYPE_MALE, GENDER_TYPE_FEMALE and GENDER_TYPE_MAX 
              QString strName;
              QString strGroupName;
              Student()
              {
                  eGender = GENDER_TYPE_MAX;
                  strName = "";
                  strGroupName = "";
              }
          } Student;
          

          Now say somewhere I have following code:

          Student stu1;
          stu1.eGender = GENDER_TYPE_MALE;
          stu1.strName = "Amit";
          stu1.strGroupName = "DOLPHIN";
          .
          .
          Student stu2;
          stu2 = stu1;
          

          Is it safe to do stu2 = stu1? Or do I need to have an assignment operator overloading for the structure?
          This doubt is related to QString assignment. As per my understanding, it is safe. But any input on the above will be a great help.

          Thanks in advance.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by JonB
          #3

          @Bikash_R_Das
          Yes you can just assign one struct from the other. As @Ben-Campbell-Wallis writes, QString does shared usage and only detaches/creates an actual copy if you change one of the QStrings. However that has nothing to do with the safety of the struct copy, and it would still be "safe" even if QString did not have this behaviour. This is down to the implementation of QString &QString::operator=(const QString &other) / QString::QString(const QString &other), and the fact that it is inside a whole struct being assigned is not important.

          1 Reply Last reply
          3
          • B Offline
            B Offline
            Bikash_R_Das
            wrote last edited by
            #4

            Thanks @Ben-Campbell-Wallis and @JonB for the reply.

            1 Reply Last reply
            1
            • B Bikash_R_Das has marked this topic as solved
            • B Bikash_R_Das has marked this topic as solved

            • Login

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