I am kinda trying to implement a windowing library in C++, the circumstances are forcing me to implement it all myself, that's not the point of this question however.

My question is: What should be in a PIMPL class? What attributes, to be more accurate. For instance, as I stated yet, I am implementing an windowing library and I have 2 kind attributes:

  • These, which describe the window (int Width, char* pTitle ect. )
  • And those, which are needed by the operating system(HWND, HDC, HGLRC for windows or XEvent, Display, Window for Linux/X11).

My current approach is, that the implementation holds all attributes, the class above, that has the PIMPL, calls getter and setter methods of the implementation in order to retrieve its attributes.

What's the right approach? Put them all in one (PIMPL-) class or split them up?

  • 1
    You mean besides pus? ;) – FrustratedWithFormsDesigner Oct 20 '14 at 16:35
  • I'm assuming by "attribute" you mean field or member or instance variable, in which case the very same page you linked already has the answer - every private field/member/instance variable. – Doval Oct 20 '14 at 16:42
  • I didn't recognize 'gnat' put a link in there, when he edited this post. – LaVolpe Oct 20 '14 at 16:45
  • if the added link isn't what you meant, consider editing to remove it and instead adding a clarification on what the question is about – gnat Oct 20 '14 at 18:20

The purpose of the PIMPL idiom is to provide a more low-level separation between the interface and the implementation* so that you can alter the implementation without affecting the interface. In particular, this means the library can retain binary compatibility, allowing users to link to a new version of the library without recompiling their executables.

What goes into the PIMPL class depends entirely on what you think is likely to change or not (and also whether binary compatibility matters to you at all).

  • If you have a member variable that is extremely essential to the class and can never change without a major redesign of the library then you should just leave it in the header to save yourself from the additional complexity (and minor performance cost).

  • On the other hand, if you want to maximize flexibility and compatibility you should hide as much as you can in the PIMPL class.

  • Thanks mate ;) So now I am aiming for 'compatibility', I think I got the gist. – LaVolpe Oct 21 '14 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.