c++ - Pimpl idiom vs Pure virtual class interface -
I was wondering what a programmer would have to do to choose either the phrase or the pure virtual class and heritage.
I understand that the Pimple idioms come with a clear extra indirection for each public law and object creation overhead.
On the other hand, the pure virtual class comes with the built-in indirection (vtable) for the inheritance implementation and I understand that an object creation is not overhead.
Edit : If you create an object from outside you will need a factory
Does the pure virtual class make less desirable?
When writing a C ++ class, it is appropriate to think if it
-
A value type
Copy is not worth the value, it is never significant, it is suitable for a key in the std :: map. Example, a "string" class, or "date" class, or "complex number" class. Examples of "copy" examples of such class are understandable.
-
An entity type
Identity is important Always pass from context, never to "value" often, understand the "copy" examples in the class at all Does not come in. When it is understood, a polymorphic "clone" method is usually more appropriate. Example: a socket class, a database class, a "policy" class, anything that would "close" in a functional language.
Both PIMPL and Pure Essence are the techniques to compile class time dependency.
However, I only use PIMPL to implement the value type (Type 1), and only occasionally when I really want to reduce the coupling and compile time dependence, It's not worth the trouble. As you say correctly, there is more syntax overhead because you have to write forwarding methods for all public methods. For Type 2 classes, I always use the pure abstract base class with the associated factory method (s).
Comments
Post a Comment