dimanche 26 janvier 2020

Unforgettable Factory: When is constructor instantiated?

I'm implementing the unforgettable factory. Everything works fine but one thing: The classes are not registered, sometimes.

I think the crucial part is the Registrar::registered member. If it is used, "The really fun part" calls the registerT function which registers the class.

In my toy example, the class is not registered unless either

  1. the constructor is implemented (in cpp-file or inline). Explicit default constructor and constructor inheritance (*) do not register the class.
  2. there is a virtual method in the base class (Animal), which is overridden in the registered class and implemented in cpp-file. Inline implementation does not work (unlike inline ctor).

Did I make a mistake or did the author miss something? In my real-world application, some classes are registered and some are not and I cannot spot the difference (all classes satisfy (1.)), that's why I must deepen my understanding.

My question is: Under which circumstances exactly is registerT called? In other words: When is the constructor of Registrar instantiated? Where should I put the (void) registered; such that it is always instantiated?

(*) to make constructor inheritance work, I made the constructors of Factory and Factory<...>::Registrar public

Aucun commentaire:

Enregistrer un commentaire