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. Returning C++ references from more programming interfaces?
QtWS25 Last Chance

Returning C++ references from more programming interfaces?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstandarditemqvectorreferencesapisoftware design
27 Posts 4 Posters 7.7k 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.
  • E Offline
    E Offline
    elfring
    wrote on last edited by
    #1

    I suggest to take another look at a known software situation:
    The class “QStandardItem” provides a programming interface for a two-dimensional table while the class “QVector” provides a programming interface for a one-dimensional data structure.

    I find it interesting then that the QVector class contains some member functions which return references to an object which was stored at a specific position in such a container. Can this functionality make sense also for other classes like QStandardItem?

    JKSHJ 1 Reply Last reply
    0
    • E elfring

      I suggest to take another look at a known software situation:
      The class “QStandardItem” provides a programming interface for a two-dimensional table while the class “QVector” provides a programming interface for a one-dimensional data structure.

      I find it interesting then that the QVector class contains some member functions which return references to an object which was stored at a specific position in such a container. Can this functionality make sense also for other classes like QStandardItem?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @elfring said in Returning C++ references from more programming interfaces?:

      I find it interesting then that the QVector class contains some member functions which return references to an object which was stored at a specific position in such a container. Can this functionality make sense also for other classes like QStandardItem?

      No, it does not make sense.

      If you add this functionality to QStandardItem, you will break encapsulation which is bad for object-oriented programming.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      E 1 Reply Last reply
      3
      • JKSHJ JKSH

        @elfring said in Returning C++ references from more programming interfaces?:

        I find it interesting then that the QVector class contains some member functions which return references to an object which was stored at a specific position in such a container. Can this functionality make sense also for other classes like QStandardItem?

        No, it does not make sense.

        If you add this functionality to QStandardItem, you will break encapsulation which is bad for object-oriented programming.

        E Offline
        E Offline
        elfring
        wrote on last edited by
        #3

        …, you will break encapsulation which is bad for object-oriented programming.

        Do you find the “encapsulation” already broken for the QVector class then?

        Will software design extensions become feasible?

        JKSHJ 1 Reply Last reply
        0
        • E elfring

          …, you will break encapsulation which is bad for object-oriented programming.

          Do you find the “encapsulation” already broken for the QVector class then?

          Will software design extensions become feasible?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @elfring said in Returning C++ references from more programming interfaces?:

          Do you find the “encapsulation” already broken for the QVector class then?

          No, because QVector is a generic container. It is designed to allow full access to every element.

          QStandardItem is not a generic container.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          E 1 Reply Last reply
          2
          • JKSHJ JKSH

            @elfring said in Returning C++ references from more programming interfaces?:

            Do you find the “encapsulation” already broken for the QVector class then?

            No, because QVector is a generic container. It is designed to allow full access to every element.

            QStandardItem is not a generic container.

            E Offline
            E Offline
            elfring
            wrote on last edited by
            #5

            QStandardItem is not a generic container.

            Can any more generic functionality be combined also with this class?

            JKSHJ 1 Reply Last reply
            0
            • E elfring

              QStandardItem is not a generic container.

              Can any more generic functionality be combined also with this class?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @elfring said in Returning C++ references from more programming interfaces?:

              Can any more generic functionality be combined also with this class?

              No. QStandardItem is a very specific class so you should not add generic functionality to it.

              If you want generic functionality, use a different class.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              2
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                Say you return a reference (to the data, I assume). And you change said data. How is the model supposed to know that the data changed. In other words, who takes care of emitting dataChanged()?
                The reference user can't be an acceptable answer as it would mean the developer has to remember to call a function every time and that's not something the current C++ evolution likes.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                E 1 Reply Last reply
                3
                • VRoninV VRonin

                  Say you return a reference (to the data, I assume). And you change said data. How is the model supposed to know that the data changed. In other words, who takes care of emitting dataChanged()?
                  The reference user can't be an acceptable answer as it would mean the developer has to remember to call a function every time and that's not something the current C++ evolution likes.

                  E Offline
                  E Offline
                  elfring
                  wrote on last edited by
                  #8

                  In other words, who takes care of emitting dataChanged()?

                  The responsibility is the same to notify others about data changes, isn't it?

                  Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

                  JKSHJ 1 Reply Last reply
                  0
                  • E elfring

                    In other words, who takes care of emitting dataChanged()?

                    The responsibility is the same to notify others about data changes, isn't it?

                    Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by JKSH
                    #9

                    @elfring said in Returning C++ references from more programming interfaces?:

                    In other words, who takes care of emitting dataChanged()?

                    The responsibility is the same to notify others about data changes, isn't it?

                    Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

                    QStandardItemModel is responsible for emitting dataChanged(). Every time you call QStandardItem::setValue(), QStandardItemModel will automatically emit dataChanged().

                    If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted. This is why we MUST NOT provide a reference to a QStandardItem's data!

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    E 1 Reply Last reply
                    5
                    • JKSHJ JKSH

                      @elfring said in Returning C++ references from more programming interfaces?:

                      In other words, who takes care of emitting dataChanged()?

                      The responsibility is the same to notify others about data changes, isn't it?

                      Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

                      QStandardItemModel is responsible for emitting dataChanged(). Every time you call QStandardItem::setValue(), QStandardItemModel will automatically emit dataChanged().

                      If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted. This is why we MUST NOT provide a reference to a QStandardItem's data!

                      E Offline
                      E Offline
                      elfring
                      wrote on last edited by
                      #10

                      Every time you call QStandardItem::setValue(), …

                      Would you like to refer to an other class (or member function) here?

                      If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted.

                      How do you think about to take additional solutions into account for this software development concern?

                      Can such customised functions trigger also the desired change notification?

                      VRoninV 1 Reply Last reply
                      0
                      • E elfring

                        Every time you call QStandardItem::setValue(), …

                        Would you like to refer to an other class (or member function) here?

                        If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted.

                        How do you think about to take additional solutions into account for this software development concern?

                        Can such customised functions trigger also the desired change notification?

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        @elfring said in Returning C++ references from more programming interfaces?:

                        Would you like to refer to an other class (or member function) here?

                        It's setData, just a typo

                        Can such customised functions trigger also the desired change notification?

                        I can't think of how but I'm happy to see an example implementation of what would work

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        E 1 Reply Last reply
                        3
                        • VRoninV VRonin

                          @elfring said in Returning C++ references from more programming interfaces?:

                          Would you like to refer to an other class (or member function) here?

                          It's setData, just a typo

                          Can such customised functions trigger also the desired change notification?

                          I can't think of how but I'm happy to see an example implementation of what would work

                          E Offline
                          E Offline
                          elfring
                          wrote on last edited by
                          #12

                          I can't think of how but I'm happy to see an example implementation of what would work

                          I am curious on how the corresponding change acceptance will evolve for possible extensions around the class “QStandardItem”.

                          jsulmJ 1 Reply Last reply
                          0
                          • E elfring

                            I can't think of how but I'm happy to see an example implementation of what would work

                            I am curious on how the corresponding change acceptance will evolve for possible extensions around the class “QStandardItem”.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @elfring I think @VRonin asked for an example, you do not have to submit a patch to Qt

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            E 1 Reply Last reply
                            2
                            • jsulmJ jsulm

                              @elfring I think @VRonin asked for an example, you do not have to submit a patch to Qt

                              E Offline
                              E Offline
                              elfring
                              wrote on last edited by
                              #14

                              … , you do not have to submit a patch to Qt

                              • Can the work with elements from a QStandardItem object become as convenient as the access for simple arrays?
                              • Are you used to the programming with template functions and overloaded operators?
                              1 Reply Last reply
                              0
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by VRonin
                                #15

                                To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

                                class Example : public QObject{
                                    Q_OBJECT
                                public:
                                    explicit Example(QObject* parent = nullptr) 
                                        : QObject(parent), m_value(0)
                                    {}
                                    const int& value() const {return m_value;}
                                    void setValue(const int& val){
                                        if(m_value==val)
                                            return;
                                        m_value=val;
                                        valueChanged(m_value);
                                    }
                                signals:
                                    void valueChanged(int val);
                                private:
                                    int m_value;
                                };
                                

                                How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                E VRoninV 2 Replies Last reply
                                1
                                • VRoninV VRonin

                                  To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

                                  class Example : public QObject{
                                      Q_OBJECT
                                  public:
                                      explicit Example(QObject* parent = nullptr) 
                                          : QObject(parent), m_value(0)
                                      {}
                                      const int& value() const {return m_value;}
                                      void setValue(const int& val){
                                          if(m_value==val)
                                              return;
                                          m_value=val;
                                          valueChanged(m_value);
                                      }
                                  signals:
                                      void valueChanged(int val);
                                  private:
                                      int m_value;
                                  };
                                  

                                  How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                  E Offline
                                  E Offline
                                  elfring
                                  wrote on last edited by
                                  #16

                                  How would you structure something like …

                                  I would expect that the member variable will be changed only if no C++ exceptions were thrown.
                                  Thus I would interpret the following approach as an useful software design option.

                                  void modify(my_data const & md)
                                  {
                                  m_value = md;
                                  emit valueChanged();
                                  }
                                  

                                  Would you like to extend such an example with function objects or the application of lambdas?

                                  1 Reply Last reply
                                  0
                                  • VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by VRonin
                                    #17

                                    m_value = md;

                                    So what's the advantage of using a reference if you still call the assignment operator (that would trigger a copy)? and what's the difference between your modify and my setValue?

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    E 1 Reply Last reply
                                    0
                                    • VRoninV VRonin

                                      m_value = md;

                                      So what's the advantage of using a reference if you still call the assignment operator (that would trigger a copy)? and what's the difference between your modify and my setValue?

                                      E Offline
                                      E Offline
                                      elfring
                                      wrote on last edited by
                                      #18

                                      … and what's the difference between your modify and my setValue?

                                      I omitted an equality check in my example.
                                      But I guess that this implementation detail distracts from the original issue of my feature request here.

                                      Would you like to adjust programming interfaces around container class variants any further?

                                      1 Reply Last reply
                                      0
                                      • VRoninV VRonin

                                        To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

                                        class Example : public QObject{
                                            Q_OBJECT
                                        public:
                                            explicit Example(QObject* parent = nullptr) 
                                                : QObject(parent), m_value(0)
                                            {}
                                            const int& value() const {return m_value;}
                                            void setValue(const int& val){
                                                if(m_value==val)
                                                    return;
                                                m_value=val;
                                                valueChanged(m_value);
                                            }
                                        signals:
                                            void valueChanged(int val);
                                        private:
                                            int m_value;
                                        };
                                        

                                        How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                        VRoninV Offline
                                        VRoninV Offline
                                        VRonin
                                        wrote on last edited by
                                        #19

                                        You didn't answer

                                        @VRonin said in Returning C++ references from more programming interfaces?:

                                        How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                        ~Napoleon Bonaparte

                                        On a crusade to banish setIndexWidget() from the holy land of Qt

                                        E 1 Reply Last reply
                                        0
                                        • VRoninV VRonin

                                          You didn't answer

                                          @VRonin said in Returning C++ references from more programming interfaces?:

                                          How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                          E Offline
                                          E Offline
                                          elfring
                                          wrote on last edited by
                                          #20

                                          You didn't answer

                                          I suggest to distinguish the update scope and the actor which should trigger the desired change notification (by a specific function call).
                                          Another software design option would be the use of a corresponding class, wouldn't it?

                                          Example demo1;
                                          
                                          struct notifier
                                          {
                                           Example& ex;
                                           
                                           notifier(Example& target, int input)
                                           : ex(target)
                                           { ex[0] = input; }
                                           
                                           ~notifier()
                                           { ex.valueChanged(); }
                                          } demo2(demo1, 123);
                                          
                                          JKSHJ 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