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. about the d-pointer,adding an api function will lead to crash?
QtWS25 Last Chance

about the d-pointer,adding an api function will lead to crash?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 428 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.
  • Q Offline
    Q Offline
    QtTester
    wrote on last edited by QtTester
    #1

    Hi ,all.

    according the wiki dpointer: https://wiki.qt.io/D-Pointer?msclkid=56fa4f8cc44611ec9a340614427a8872
    the code:

     class WidgetPrivate;
     
     class Widget
     {
         // ...
         Rect geometry() const;
         // ... 
     
     private:
         WidgetPrivate *d_ptr;
     };
    

    but ,if we add a new api, should above code work still?

     class WidgetPrivate;
     
     class Widget
     {
         // ...
         Rect geometry() const;
         // ... 
         int sum(); // new API
     private:
         WidgetPrivate *d_ptr;
     };
    

    If not ,how about put the dpointer to the first place?

    class WidgetPrivate;
     
     class Widget
     {
    private:
        WidgetPrivate *d_ptr;
    public:
         // ...
         Rect geometry() const;
         // ... 
         int sum(); // new API,will it work for compatible?
     private:
         // none
     };
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @QtTester said in about the d-pointer,adding an api function will lead to crash?:

      but ,if we add a new api, should above code work still?

      When you add a new function the memory footprint (and therefore layout) does not change - it's still X bytes in your case. 'Just' the order of functions may change when you don't add it at the back but even this doesn't matter since the loaded is resolving the symbols during runtime. The only problem is when you add a virtual function since then the size of the vtable changes and a derived class compiled against the older version will go havoc.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Q 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        @QtTester said in about the d-pointer,adding an api function will lead to crash?:

        but ,if we add a new api, should above code work still?

        When you add a new function the memory footprint (and therefore layout) does not change - it's still X bytes in your case. 'Just' the order of functions may change when you don't add it at the back but even this doesn't matter since the loaded is resolving the symbols during runtime. The only problem is when you add a virtual function since then the size of the vtable changes and a derived class compiled against the older version will go havoc.

        Q Offline
        Q Offline
        QtTester
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        thanks for replying.
        That's to say: add a new variable will increase the memory size,but an api will not?
        is thera a link to show why?
        And, if we add a virtual function just at the back ,will it work compatible?

        Christian EhrlicherC 1 Reply Last reply
        0
        • Q QtTester

          @Christian-Ehrlicher
          thanks for replying.
          That's to say: add a new variable will increase the memory size,but an api will not?
          is thera a link to show why?
          And, if we add a virtual function just at the back ,will it work compatible?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @QtTester said in about the d-pointer,adding an api function will lead to crash?:

          That's to say: add a new variable will increase the memory size,but an api will not?

          Yes because the object doesn't need more memory when a new function is added - why should it?

          And, if we add a virtual function just at the back ,will it work compatible?

          As I said above - adding a virtual function will kill your classes derived from this class.

          Why do you have such a hard binary compatibility requirement? If you really have you should take a deeper look into this before starting. It's not that easy as it seems (and mostly not needed for simple projects).

          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
          0

          • Login

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