필터 지우기
필터 지우기

Object-Oriented Programming Onramp, Overloading Operators

조회 수: 8 (최근 30일)
Elisabeth
Elisabeth 2024년 6월 13일
댓글: Elisabeth 2024년 6월 18일
Hallo Harald,
meine Frage bezieht sich auf die Erläuterungen auf dieser Seite:
Mit x und y werden zwei Instanzen der Klasse foo erzeugt. Anschließend werden beide multipliziert. Wo wird die Information in der Tabelle (Zuordnung der Operatoren zu den Funktionen) hinterlegt?
Vielen Dank!
Lisa

채택된 답변

Vandit
Vandit 2024년 6월 17일
Hello Elisabeth,
The information about how operators are associated with functions in MATLAB is part of MATLAB's object-oriented programming design. When you define a class, you can overload built-in MATLAB functions and operators for instances of that class by implementing methods with specific names. The table provided shows some of these associations:
  • a + b is associated with the plus(a, b) method.
  • a - b is associated with the minus(a, b) method.
  • a .* b is associated with the times(a, b) method.
  • a * b is associated with the mtimes(a, b) method.
  • a < b is associated with the lt(a, b) method.
  • a == b is associated with the eq(a, b) method.
In the context of the 'foo' class you provided, when you perform the operation z = x * y, MATLAB internally calls the "mtimes" method defined in your class (since * is associated with "mtimes"). This is because the "mtimes" method is specifically designed to handle the * operator for objects of your class. The "mtimes" method you've implemented in turn calls the "times" method (via the .* operator within the "mtimes" method), which performs element-wise multiplication of the 'Values' properties of the two 'foo' objects and returns a new 'foo' object with the result.
For a comprehensive listing of all MATLAB operators, symbols, and special characters, along with their corresponding functional equivalents, please refer to the following documentation:
Hope this helps.
  댓글 수: 1
Elisabeth
Elisabeth 2024년 6월 18일
Hello Vandit,
thank you for you answer! I understand that somewhere in a code the behaviour of operators must be defined (in that case by special functions to customize the behaviour of different operators like +,-,...). I don't know how to classify Matlab compared to C and C++. C++ supports operator overloading but Matlab is based on C, right? (What exactly is Matlab, since its based on C but not C++ but knows classes?) Can I look up the implementation of the table, you've mentioned above, somewhere?
Elisabeth

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!