سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top