Pimpl idiom

Peter K├╝mmel syntheticpp at gmx.net
Sun Feb 12 20:10:14 GMT 2006


Hello,

I've read this in the TODO file of kdelibs:

- Change all FooPrivate *d; -> Private * const d; and place initialization
  in the constructor (for classes that would benefit from this).  To help catch silly
  mistakes since d should never change. Also consider changing to use KStaticDeleter to
  help prevent mistakes where developers forget to delete the pointer. Maybe make use of
  Qt4 helper macros?
  (Frerich)


The problem with such a solution is:
- you can forget to create the private class
- you can forget to delete d
- using a pure pointer for the pimpl violates const correctness.
  It is possible to call non const member functions of the implementation object


Attached a proposal for a different implementation of the Pimpl idiom,
which has not above problems.

It automatically creates on construction, and deletes on destruction the
private implementation object, it also propagates the constness to the
private object.
It is pure C++ and needs no macros or static variables.


Cheers,
Peter


See also:
http://www.gotw.ca/publications/mill04.htm
http://www.gotw.ca/publications/mill05.htm
http://boost-consulting.com/vault/



-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Makefile
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20060212/44dd1157/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: main.cpp
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20060212/44dd1157/attachment-0001.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: KImpl.h
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20060212/44dd1157/attachment.h>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: type.h
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20060212/44dd1157/attachment-0001.h>


More information about the kde-core-devel mailing list