title: How can I create a UE4 UCLASS base class that uses CreateDefaultSubobject() but allow sub-classes to change the type of the sub-object?
body: <p>I am writing a re-usable RPG framework with the UE4 Gameplay Ability System. As part of the framework, I want to provide a character base class that games can override. The base class should instantiate both the Ability System Component (ASC) and Attribute Set (AS) of the character as sub-objects, preferably via the <code>CreateDefaultSubobject()</code>, which, as you may know, can only be invoked in the constructor. The problem is that I need the type of the ASC and AS to be something that sub-classes can override, so if they have a more specific ASC and AC that can plug it in.</p>
  <p>Here are the things I've tried/constraints I need to satisfy:</p>
  <ul>
  <li><code>UCLASS()</code> objects cannot be templated, so I can't make the type of the ASC and AS class template variables.</li>
  <li>If I define function templates on the constructor, there's no way to supply the values for those templates when the base class constructor is being invoked because C++ only allows function templates to be supplied when a function has a name, and constructors don't have a name (only the name of the class), so the compiler flags a syntax error if I try to supply the type parameters when invoking the parameterized base class constructor.</li>
  <li>I can't move the <code>CreateDefaultSubobject()</code> calls into the initializers for the constructor because doing so causes a crash. I suspect the initializer list is evaluated before the object is constructed and the <code>CreateDefaultSubobject()</code> tries to access some of the state of the parent character. The objects must be created as part of the body of the constructor.</li>
  <li>I can't move the logic that creates the ASC and AS into a virtual method that the parent class calls and sub-classes override because that would be unsafe and lead to only the parent's version of the virtual methods getting called (if at all) due to the way that classes are constructed.</li>
  <li>I can't have the child class simply overwrite the ASC and AS that the parent created with its own copies because the ASC and AS have already been added as child components with the same name to the parent.</li>
  </ul>
  <p>How can I satisfy these constraints and make it possible for child-classes to provider their own types for the ASC and AS?</p>