Copying From QString To QString



  • 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;
    }
    @



  • 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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.