Why use interface instead of pimpl

The opaque pointer (pimpl) idiom has been inherited from C language where it is used to encapsulate implementation details. However, both old-school and "modern" C++ dispence you from writing some ugly code, and allow to use interfaces with object factories.

The example of implementation with both pimpl and interface approach is as follows.

static_cast vs dynamic_cast: undefined behavior

Do not use static_cast when you cast from a basic class to a derived one. This may lead to undefined behavior. To handle the polymorphism, a virtual inheritance or a multiple inheritance case always use dynamic_cast instead.

The following short example shows the undefined behavior cases. This works with GCC, VC and online C++ shells:


Subscribe to RSS - C++