MATLAB: How to combine two objects of different subclasses into the property of another class

classconstructorinheritMATLABoopsubclass

Hello,
I explain the example so that it is better understood: I have a "board" class with a "player" attribute. This attribute is a list of different objects of the "human player" class. If I want to add to this list "player" objects of the class "player cpu" I get an error.

Best Answer

Your classes need to derive from the matlab.mixin.Heterogeneous class.
Specifically, your player class needs to derive from matlab.mixin.Heterogeneous. Then, as long as all your other player classes derive from your main player base class, you should be all set.
Check out this documentation page for more details: Designing Heterogeneous Class Hierarchies