Results 1 to 7 of 7

Thread: Q_declare_private()

  1. #1
    Join Date
    Jul 2008
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Q_declare_private()

    I'm just looking at some example QT code, and I notice that someties in the header file there will be something like so:

    Qt Code:
    1. class MyClassPrivate;
    2.  
    3. class MyClass : public QObject
    4. {
    5. public:
    6. MyClass( QObject *parent = 0 );
    7. ...other non relevant stuff...
    8. protected:
    9. MyClassPrivate *const ptr;
    10. MyClass( MyClassPrivate &the_ptr, QObject *parent = 0 );
    11. private:
    12. Q_DECLARE_PRIVATE( MyClass );
    13. };
    To copy to clipboard, switch view to plain text mode 

    I'm just curious what does this "macro" do? Does it offer the ability to have a private version of the class? Why do they want a separate Protected Constructor? Can somebody give me an example why it is useful?

    Thanks,

    Michael
    Last edited by marf; 19th July 2008 at 18:22. Reason: updated with constructors

  2. #2
    Join Date
    Jun 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Q_declare_private()

    It expands to:
    Qt Code:
    1. #define Q_DECLARE_PRIVATE(Class) \
    2. inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(d_ptr); } \
    3. inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(d_ptr); } \
    4. friend class Class##Private;
    To copy to clipboard, switch view to plain text mode 
    It defines a d_func() which returns d_ptr (pointer to private class object).
    It is used internally in QT, so it's not documented and you are not supposed to use this.

    If you want to implement implicit sharing, look forward to QSharedData and QSharedDataPointer classes.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Q_declare_private()

    See also Qt Centre wiki: [WIKI]Private implementation[/WIKI].
    J-P Nurmi

  4. #4
    Join Date
    Jul 2008
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q_declare_private()

    Thanks, I found that and it wasn't very useful.

    However I stumbled upon this

    http://techbase.kde.org/Policies/Lib...icy#D-Pointers

    and

    http://techbase.kde.org/Policies/Lib...ointer_Example

    which bring alot of clarity to the issue.

    However I have another question now.

    In the second link above the example will basically never have private member variables in the 'KFooBase' but rather put them into the public: 'KFooBasePrivate' class.

    also in this part of code

    Qt Code:
    1. class KFooBasePrivate
    2. {
    3. public:
    4. virtual ~KFooBasePrivate() { }
    5. int someInteger;
    6. };
    To copy to clipboard, switch view to plain text mode 

    why not do this

    Qt Code:
    1. class KFooBase
    2. {
    3. public:
    4. ...the same as example...
    5. private:
    6. //Get rid of this stuff
    7. //friend class KFooBasePrivate;
    8. //inline KFooBasePrivate *d_func() { return d_ptr; }
    9. //inline const KFooBasePrivate *d_func() const { return d_ptr; }
    10. int someInteger;
    11. };
    To copy to clipboard, switch view to plain text mode 

    I guess at the moment, I don't see the reason to go through all this trouble to make a "Private" class, to have member variables in there, when perhaps you could have just ignored this d_ptr stuff and used the private tag? Through lots of my reading today i read that it is best to use this method and not put private member variables in the private: part of a class, but they gave me no reason.
    Last edited by marf; 20th July 2008 at 22:27. Reason: added code tags

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Q_declare_private()

    For libraries like Qt it is very important to retain binary compatibility so that existing applications keep working even if the library is updated. Just imagine the hassle if everyone had to recompile every single Qt application whenever the Qt library itself was updated. PIMPL makes it possible to introduce new functionality without breaking the ABI.

    See also TT Knowledgebase: You frequently say that you cannot add this or that feature because it would break binary compatibility. What does this mean, really?
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Q_declare_private()

    My wwWidgets use p-impl and Q_DECLARE_PRIVATE, so you can study the source to see what is does.

  7. #7
    Join Date
    Jul 2008
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q_declare_private()

    jpn and wysota,

    thanks for all your help so far. I have used your wwWidgets in combination with the KDE policy link I used above and I have made a test project. It doesn't want to set my integer for some reason, I commented the line that is crashing the program.

    If one of you could be so kind to check out my little test program, that'd be great.

    http://rapidshare.com/files/13145706...pl.tar.gz.html
    Last edited by marf; 21st July 2008 at 23:10. Reason: updated link

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.