Pimpl (Opaque Pointers)

Summary Pimpl is used to hide implementation details for library consumers. It hides headers, data members, as well as private functions. Note that ~foo() has to be defined after foo::impl gets fulfilled. This is a requirement from unique_ptr: std::unique_ptr may be constructed for an incomplete type T, such as to facilitate the use as a handle in…

Virtual Functions, Constructors, and Destructors

Summary In this post, I will introduce rules related to constructors and destructors of classes which has virtual functions. Conclusions Golden Rule 1: Define a virtual destructor immediately Golden Rule 2: Do not invoke virtual functions from constructors or destructors Details In C++, Virtual functions allow for the choice of member function calls to be…

Class Template Definition and Declaration

Summary In this post. I will introduce class templates in C++. Especially, how do we declare a template which generates classes and how do we write definitions in separate source files. I will also dive deeper to present how class templates are compiled. Conclusion As usual, I present the conclusions first.Say the customer will use…