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. String and implicit sharing
Forum Updated to NodeBB v4.3 + New Features

String and implicit sharing

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 143 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.
  • T Offline
    T Offline
    TomNow99
    wrote on 23 Aug 2023, 15:36 last edited by TomNow99
    #1

    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.

    C 1 Reply Last reply 23 Aug 2023, 16:42
    0
    • T TomNow99
      23 Aug 2023, 15:36

      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.

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 23 Aug 2023, 16:42 last edited by
      #2

      As long as you don't access the object via a non-const function there is only one QString instance with '"some Text"'.

      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

      1/2

      23 Aug 2023, 15:36

      • Login

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