Your code is actually not C ++ 11. I thought that move constructors could work here, since you essentially move one A to another, and then slightly modify it.
As with C ++ 03, you can optimize the initialization that you want to execute once in all your constructors, or by placing them in a subclass or base class (often with protected or private inheritance, since it is an implementation detail). Using the base class:
class ABase { protected: int num; ABase() : num(42) {} }; class A : protected ABase { public: A() = default;
(You can change your access rights to taste). The problem here is that I ever create a single "ABase" object, and if it has more than just a trivial int member, it can be significant. I really like inheritance, because then I use it as a member of the class, and not as a member of any aggregate object, and I prefer the inheritance to be protected or closed here, but sometimes, if the base class has members, I want to be public, I use public inheritance, but give the base class a protected destructor. This assumes that there is no v-table, and therefore no further output is expected. (You can finalize A here, actually making inheritance virtual and private, but you probably don't want to).
source share