Why is that in every class in MATLAB I must use "this"? I think that in C++ I don't need to use "this", only if I want to. Is this also the case in MATLAB?
obj = class(s,ClassName) creates an array of objects of the specified class using the struct s as a pattern to determine the size of obj .
MATLAB defines fundamental classes that comprise the basic types used by the language. These classes include numeric, logical , char , cell , struct , and function handle.
What Are Static Methods. Static methods are associated with a class, but not with specific instances of that class. These methods do not require an object of the class as an input argument. Therefore, you can call static methods without creating an object of the class.
A value class constructor returns an object that is associated with the variable to which it is assigned. If you reassign this variable, MATLAB® creates an independent copy of the original object. If you pass this variable to a function to modify it, the function must return the modified object as an output argument.
In short, you must use some kind of explicit reference.
First of all, unlike in C++/C#/Java where it is named this, you can use any name you want. The reason that you must use explicit calls is Matlab designers decision. The idea was to support Matlab vector operations on objects, as if they are structs. The following is a fragment from the link above:
While languages with an implicit object parameter provide a "this" keyword to access the implicit object, they usually do not require you to access a property through "this". If MATLAB had implicit properties, the logical extension to array-based objects would be to index into nothing:
S = S + (k).Value;
Edit: Following the good comment of @AndrewJanke, I would like to add that MATLAB could have had this as implicit reference, and only force to use it in indexing of array-based objects. Nevertheless, this approach was not chosen by MATLAB designers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With