to ensure the ABI with the pimpl pattern, is that true that we only need to put all the data members to the "Private class" ? I see in some introduction about pimpl, they also make all the functions implementations in the "Private class" and define a "wrapper function" on the exported class for each function in the "Private class", is that necessary?
2 Answers
When your aim is to have ABI compatibility, then you need to put all data member variables in the private class.
There is this famous article about c++ ABI compatibility. From their to do's/don'ts :
- you can not change the signature of existing member functions. You can add new methods
- you can not add virtual functions to a class without one
- you can not add or change non-static data members, or change their order
The list goes on, but these 3 items should cover your questions.
As long as you are 100% sure you are not going to modify private member functions, it is fine. If you expect a change, move them to a private class. I always expect a change.
Comments
The advantage of wrapping the functions is that then the functions can be defined in the "private" aka "implementation" class. (Although I always use a struct. Since the entire thing is private to one file I don't see the point to protecting its implementation.)
Since the functions are defined in the same class as the data, you don't need to stick p-> on the front of every access like p->variable.
If you put data in the implementation class/struct but no functions, then all of your function definitions need to put that p-> before every access.