MATLAB: Do I sometimes receive an error concerning consistency of sizes between parent and child objects, depending on the version of MATLAB

classclassesinheritinheritanceinheritenceMATLABnon-scalarobjectobjects

I have a parent class, "foo", from which a child class, "bar", inherits properties and methods.
In bar's constructor, I receive an error when the CLASS function is called:
??? Error using ==> class
Size of struct array being made into an object and the parent objects must be consistent.
Error in ==> bar.bar at 5
b = class(s, 'bar', f);
In this case, "f" is my parent object (of type foo), "b" is the new object being created (of type "bar"), and "s" is the structure that defines the non-inherited data fields of "b".
This error seems inconsistent between different versions of MATLAB.

Best Answer

When using syntax such as
obj = class(s, 'class_name', parent1, parent2, ...)
"obj" will inherit fields and methods from "parent1", "parent2", and so on.
Field values are inherited in an element-wise fashion. The size of each of these parent objects must match the size of the template structure, unless the parent object is a scalar (1x1), in which case scalar expansion is performed. Note that scalar expansion is not performed on "s".
A statement such as
obj = class(struct([]), 'class_name', parent1, parent2, ...)
is used to inherit from multiple parents without adding any fields beyond those of the parents; this is a special case that violates the size rules described above. The parent objects cannot be empty. In this case, the non-scalar parent objects must have a common size, which dictates the size of the resulting object.
Since MATLAB R14, 0x0 objects have been allowed. When these objects are created by inheriting from a parent object, the size of the field-structure and the parent objects must be consistent with the rules above. However, due to a bug in the size-checking mechanism used by the CLASS function, from R14 to R2006a, MATLAB would erroneously allow the creation of a class that combines a 1x1 structure and a 0x0 parent object.