I am also wondering if this is suitable as an alternative to a smart pointer.
But, IMO, to be a smart pointer, a class must be used as a pointer, i.e.
SmartPtr<int> ptr = new int(42); int x = *ptr;
So yes, this is a kind of memory management, but it is not a smart pointer, because it does not have pointer semantics.
As mentioned in the comments, the pimpl idiom is really useful for maintaining compatibility, and it can also improve development efficiency, since you don't need to recompile the class containing the class. BUT, in order to have the last advantage, you do not have to define the inner class (i.e. Data) inside the parent class, but just simply put the declaration in front and put the actual definition inside another header.
class Fred { ... private: class Data; };
And I believe that for future development it is not useful to declare a data variant inside the Fred class, because if you need to add another class, you will need to modify Fred, and not just create another class. This may be required, but I suggest you avoid this part.
If I didnโt understand anything, feel free to ask questions!
fstamour
source share