Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Copying From QString To QString

    General and Desktop
    2
    3
    1448
    Loading More Posts
    • 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.
    • E
      EverydayDiesel last edited by

      Hello,

      I have a class that I am trying to add get/set access modifiers to but cant seem to get it to compile.

      Ultimately what I am asking is, in this case is it best to allocate on the heap? How would you write this code for your class?

      MyWidget.h
      @
      class MyWidget
      {
      public:
      QString GetGroupName();
      void SetGroupName(QString sVal)

      private:

      QString m_sGroupName;
      

      };
      @

      MyWidget.cpp

      @
      QString MyWidget::GetGroupName()
      {
      return m_sGroupName;
      }

      void MyWidget::SetGroupName(QString sVal)
      {
      m_sGroupName = sVal;
      }
      @

      1 Reply Last reply Reply Quote 0
      • M
        MuldeR last edited by

        I think it's probably the best, performance-wise, to use const references here:
        @class MyWidget
        {
        public:
        const QString& GetGroupName(void) const;
        void SetGroupName(const QString &sVal);

        private:
        QString m_sGroupName;
        };

        /* ----------------- */

        const QString& MyWidget::GetGroupName(void) const
        {
        return m_sGroupName;
        }

        void MyWidget::SetGroupName(const QString &sVal)
        {
        m_sGroupName = sVal;
        }@

        Anyway, passing QString "by value" is okay too, because QString uses "implicit sharing":http://qt-project.org/doc/qt-4.8/implicit-sharing.html, which makes copying a QString a rather cheap operation.

        BTW: Why does your code not compile? What is the error message?

        bq. Ultimately what I am asking is, in this case is it best to allocate on the heap?

        Nope.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply Reply Quote 0
        • E
          EverydayDiesel last edited by

          The code above fixed the errors i was having. Thanks alot!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post