I have two classes (let’s call them A and B), both inheriting from a common superclass (C).
Let’s say I need to build a List of Cs. My problem is that, depending on the data received by C‘s constructor I need to put an A or B object inside the list.
Is there a way to do this from inside C‘s constructor? Or, alternatively, how can I solve this issue?
This sounds like a good place to use a factory pattern. Basically, create a class CFactory with a static method that has a return type of C. That method can then decide to create either an A or a B and return that.