Does MATLAB call the double constructor on a=a+1?

1 回表示 (過去 30 日間)
Jan Siegmund
Jan Siegmund 2020 年 4 月 22 日
コメント済み: Jan Siegmund 2020 年 4 月 25 日
I want to overload the double class and add some properties to it.
And i also have to override the plus function. A similar code could
look like this:
classdef myclass < double
properties (Access = protected)
foo
end
methods (Access = public)
function obj = myclass(val,foo)
obj = obj@double(val);
obj.foo = foo;
end
function out = plus(obj,in)
out = myclass(plus@double(obj,in),obj.foo);
end
end
end
However, if out = in on plus, I could just obj.foo = in.foo and call plus@double(obj,in), could I?
How does MATLAB itself determine, if a plus is in fact an increment, so out = in?

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 22 日
How does MATLAB itself determine, if a plus is in fact an increment, so out = in?
Reference counting.
I do not know about class methods in this regard.
Outside of classes, in-place operations are only done inside a function of the form
function Variable = FunctionName(arguments that include Variable)
and only if the reference count for Variable is 1, and only if the outside output variable name is the same as the input variable name
In-place operations are not even done for simple code like
count = 0;
for K = 1 : 5
count = count + 1;
end
it would have to be something like
count = 0;
for K = 1 : 5
count = PlusPlus(count);
end
function var = PlusPlus(var)
var = var + 1;
end
For more discussion of in-place operations, I recommend searching postings from James Tursa
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 4 月 24 日
Mind you, exactly what the Execution Engine optimizes to in the case where it detects built-in arithmetic types and non-overloaded operations is unknown.
Jan Siegmund
Jan Siegmund 2020 年 4 月 25 日
The pain of closed source -.-

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by