Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of "this" in every MATLAB class

Tags:

matlab

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?

like image 743
Lee Avatar asked Jan 16 '12 19:01

Lee


People also ask

What does class () do 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 .

What are the different classes in MATLAB?

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 is a static method MATLAB?

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.

What is a value class in MATLAB?

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.


1 Answers

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.

like image 147
Andrey Rubshtein Avatar answered Oct 13 '22 19:10

Andrey Rubshtein