How does one go about assigning to an object's superclass?
Example:
classdef foo < bar
...
methods
function this = barSet(this,b)
% Here I want to arrange that bar(this) == b
end
end
end
How (or can) I implement barSet
?
How does one go about assigning to an object's superclass?
Example:
classdef foo < bar
...
methods
function this = barSet(this,b)
% Here I want to arrange that bar(this) == b
end
end
end
How (or can) I implement barSet
?
When messing around with the base class, you should use the @
-syntax (read about it here). For example:
classdef foo < bar
...
methods
function this = barSet(this,b)
%# call method in bar
barSet@bar(this, b);
...
end
end
end
The method barSet
is a method in both foo
and bar
.
This is Matlab's way of extending overloaded methods: if you omit the barSet(this,b)
definition in foo
, calling
F = foo;
F.barSet(5);
will call the method barSet
in class bar
with the argument 5
. If you define it, the method can do something else entirely, or first do what the version in bar
does, followed by more specialized stuff (written above as the '...'
).
Now does this help at all? Because it's not entirely clear to me what you want. I don't understand what you mean by bar(this) == b
-- is b
an instance of foo
or bar
? And what do you mean with bar(this)
? A copy constructor? So do I understand you correctly that you mean that you want to overwrite the this
instance with the b
instance to it?