MATLAB Constructors

I am having a hard time deciding what is the best way to use constructors in MATLAB. Most of the examples given:

classdef ClassA < handle
    properties
        m_a;
        m_b;   
    end 

    methods
        function obj = ClassA(a,b)
            obj.m_a = a;
            obj.m_b = b;          
    end 
end

      

But suppose a and b are from other classes ClassB and ClassC

Then the constructor can also be written as:

methods
    function obj = ClassA(a,b)
         obj.m_a = ClassB(a);
         obj.m_b = ClassC(b);          
end 

      

Which way would be the preferred convention?

+3


source to share


1 answer


In this case, there is no preferred agreement. Although they look the same, your two examples are not exactly equivalent. Everything will depend on how a

they b

will be presented to the designer classA

.

In your first case: a

and b

should be respectively classB

and classC

when they go to classA

.

In your second case: a

and b

are not instances classB/C

, but just a typical parameter required by the constructor classB/C

to create objects. Let's say they have a class double

for an argument.

So,

     case 1:                    case 2:
variable | type            variable | type
    a      classB              a      double
    b      classC              b      double

then in classA
 obj.m_a   classB           obj.m_a   classB
 obj.m_b   classC           obj.m_b   classC

      

As you can see, at the end for classA

it is not much different. The choice should be more driven by what the calling function (the function that will call the constructor classA

) should handle .

  • If you are calling a function, use a

    both b

    as an object of the class classB

    and classC

    , then it makes sense that the calling function has those already present, and then sends that to the constructor classA

    when it calls it. In this case, you can choose your first option.

  • If the caller has nothing to do with classB

    and classC

    (these classes are only useful for or internally classA

    ) then there is no need for the caller function to handle these types. Keep a

    and b

    as simple as you need and create an instance of classB

    , and classC

    in the constructor classA

    , as in your second example.




As a side note, pay attention to some details handle classes

. Specifically, the Matlab doc says:

Initialization of properties for unique values . MATLAB assigns properties to the specified default values ​​only once when MATLAB loads the class definition. Therefore , if you initialize a property value using the constructor of the handle class, MATLAB calls that constructor only once, and each instance refers to the same handle object . If you want a property value to be initialized with a new instance of the handle object every time you create the object, assign the property value to the constructor.

This means that if you choose your option 2 and classB/C

are also a descriptor class that does not require an input argument, DO NOT try to shortcut and declare them like this:

classdef ClassA < handle
    properties
        m_a = ClassB() ;    %// never do that if ClassB is a handle class
        m_b = classC() ;    %// never do that if ClassC is a handle class
    end 
methods
    function obj = ClassA(a,b)
         %// do you other stuff
end 

      

Instead, initialize the variable in the constructor (as in your example) rather than in property definitions. I know this is not what you did, but if you choose your option 2 it would be easy to fall into this trap to keep things simple. If you do this, no error will appear, but any other instance classB

you might declare / create in later code will always refer to the same object that was created the first time in your property definition. (Even other instances classA

will have their properties referencing the same unique object classB

.)

0


source







All Articles