String and implicit sharing
-
wrote on 23 Aug 2023, 15:36 last edited by TomNow99
Hi,
I would like to know, if I use implicit sharing every time.
So I have 3 classes:
class A { public: QString getCurrentString() { return currentString; } void setCurrentString(QString str) { currentString = str; } private: QString currentString; } class B { public: B(A *a) { currentA = a; } void createString() { QString someString = "some Text"; currentA->setCurrentString(someString); } private: A *currentA; } class C { public: C(A *a) { currentA = a; } void useString() { QString str = currentA->getCurrentString(); // here some use for example otherClass->useString(str); } private: A *currentA; }
I have only one object of class C and I give that object C to constructor A and B. Next I create string in class B ( createString method ) and in other place I use that string in class C ( useString() ). And my question: I create that string only in one place and in other places I only implicit sharing it? Of course I don't change string. I don't want other deep copies.
-
Hi,
I would like to know, if I use implicit sharing every time.
So I have 3 classes:
class A { public: QString getCurrentString() { return currentString; } void setCurrentString(QString str) { currentString = str; } private: QString currentString; } class B { public: B(A *a) { currentA = a; } void createString() { QString someString = "some Text"; currentA->setCurrentString(someString); } private: A *currentA; } class C { public: C(A *a) { currentA = a; } void useString() { QString str = currentA->getCurrentString(); // here some use for example otherClass->useString(str); } private: A *currentA; }
I have only one object of class C and I give that object C to constructor A and B. Next I create string in class B ( createString method ) and in other place I use that string in class C ( useString() ). And my question: I create that string only in one place and in other places I only implicit sharing it? Of course I don't change string. I don't want other deep copies.
As long as you don't access the object via a non-const function there is only one QString instance with '"some Text"'.
1/2