0

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?

chappjc
  • 30,359
  • 6
  • 75
  • 132
lsfinn
  • 446
  • 2
  • 4

1 Answers1

0

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?

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Thanks for taking a look at this. Your suggestion requires that I modify the superclass bar to add a method barSet. I'd prefer to avoid messing with the superclass in this way. What I meant by bar(this) (in bar(this) == b) was the cast of this to class bar. Similarly, it was intended that b be of class bar. – lsfinn Oct 05 '12 at 20:26