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. Copying From QString To QString

Copying From QString To QString

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.9k Views 1 Watching
  • 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 Offline
    E Offline
    EverydayDiesel
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      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
      0
      • E Offline
        E Offline
        EverydayDiesel
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

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