Results 1 to 8 of 8

Thread: PIMPL, what do I miss?

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default PIMPL, what do I miss?

    Hi,

    lately I make myself familiar with the PIMPL idiom. And all examples do in the private declaration something like this:
    Qt Code:
    1. #define PIMPL_DECLARE_PRIVATE(Class) \
    2. private: \
    3. inline Class##Private* d_func() {return reinterpret_cast<Class##Private*>(dPtr);} \
    4. inline const Class##Private* d_func() const {return reinterpret_cast<const Class##Private*>(dPtr);} \
    5. void* dPtr;
    To copy to clipboard, switch view to plain text mode 
    And assuming that you only work with normal classes: Why they all use reinterpret_cast? I don't see the need even if reinterpret_cast is cheap. What's wrong with:
    Qt Code:
    1. #define PIMPL_DECLARE_PRIVATE(Class) \
    2. private: \
    3. inline Class##Private* d_func() {return dPtr;} \
    4. inline const Class##Private* d_func() const {return const_cast<const Class##Private*>(dPtr);} \
    5. Class##Private* dPtr;
    To copy to clipboard, switch view to plain text mode 
    Do I miss something important here?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PIMPL, what do I miss?

    I'm not exactly sure what the purpose is. In pimpling, I usually don't use the macros and just take the following approach:
    Qt Code:
    1. class MyClassPrivate;
    2.  
    3. class MyClass
    4. {
    5. // ...
    6. private:
    7. MyClassPrivate *d;
    8. };
    To copy to clipboard, switch view to plain text mode 

    This suits me fine and as far as I can see has the same effect as using the above macros.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: PIMPL, what do I miss?

    My guess is that its because reinterpret_cast guarantees the original type when you cast back.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    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: PIMPL, what do I miss?

    I think a simple "return dPtr" would not work as "void*" and "WhatEverPrivate*" are not type-compatible. It might have worked in C but not C++. Same goes for the second cast -- there is no implicit cast between void* and other pointer types. I guess reinterpret_cast might be replaced by static_cast as well. These are just guesses though, I never understood the differences between some cast types (especially reinterpret_cast).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: PIMPL, what do I miss?

    Quote Originally Posted by wysota View Post
    I think a simple "return dPtr" would not work as "void*" and "WhatEverPrivate*" are not type-compatible. It might have worked in C but not C++.
    Yes, but therefore I would change the private pointer from void* to WhatEverPrivate* (dPtr in my case). So what I see "they" need the cast because the declare a void pointer. But if you declare direct a WhatEverPrivate pointer there is no need to cast. (only the const of course.)

    @high_flyer That should only be a problem when you use dynamic_cast, because it can return a NULL pointer. But I am not sure...

    @franz I want to use templates to be flexible and to add more stuff later if needed. And it not just one class.

  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: PIMPL, what do I miss?

    Maybe my initial response (which I deleted and instead wrote what I have written) would be closer to truth. The reinterpret_cast might serve as a trick to have a pimpl class that does not inherit from the pimpl class of the base class of the public class.
    Qt Code:
    1. class APrivate {};
    2. class A {
    3. private:
    4. APrivate *d;
    5. };
    6.  
    7. class BPrivate {}; // doesn't inherit APrivate
    8.  
    9. class B : public A {
    10. private:
    11. BPrivate *d;
    12. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: PIMPL, what do I miss?

    a pimpl class that does not inherit from the pimpl class of the base class of the public class.
    LOL.
    I had to read it several times to get it!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: PIMPL, what do I miss?

    a little off-topic question but here it goes.

    PIMPL says that your main class should only contain public functions and only a single private d pointer.

    But how you guys handle the case when i need some *private* slots in my main class. As moc only reads .h files ( it can parse .cpp as well but i dont want that)
    i need to have the private slots in my main class as well.. this makes my class a little bit more subject to change.

    Qt sources solves this by declaring the private class in "*_p.h" files. But then they put a BIG warning on those private headers. But developers as we know
    push the limits and include the private headers for some real hack ( there is a example of this in QtCentre Wiki as well !! )

    Anyone done this differently?

Similar Threads

  1. Replies: 0
    Last Post: 31st January 2011, 11:14
  2. When to use pimpl ?
    By Gopala Krishna in forum General Discussion
    Replies: 17
    Last Post: 16th July 2009, 23:14
  3. Segfault while using Pimpl idiom with Qt types.
    By darkadept in forum Qt Programming
    Replies: 11
    Last Post: 13th June 2009, 00:53
  4. Do you use Pimpl Idiom?
    By ComaWhite in forum General Discussion
    Replies: 5
    Last Post: 25th March 2009, 09:48
  5. Replies: 3
    Last Post: 14th September 2007, 09:41

Tags for this Thread

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.