QString to QString assignment
-
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.
-
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.
@Bikash_R_Das
Yes you can just assign onestruct
from the other. As @Ben-Campbell-Wallis writes,QString
does shared usage and only detaches/creates an actual copy if you change one of theQString
s. However that has nothing to do with the safety of thestruct
copy, and it would still be "safe" even ifQString
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 wholestruct
being assigned is not important. -
@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:
-
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.
@Bikash_R_Das
Yes you can just assign onestruct
from the other. As @Ben-Campbell-Wallis writes,QString
does shared usage and only detaches/creates an actual copy if you change one of theQString
s. However that has nothing to do with the safety of thestruct
copy, and it would still be "safe" even ifQString
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 wholestruct
being assigned is not important. -
Thanks @Ben-Campbell-Wallis and @JonB for the reply.
-
-