How to overload .( ) operator (subsref) for static methods?

3 views (last 30 days)
Hi everyone,
I was trying to overload some builtin methods inside a class. Namely, I was looking at how to overload the behaviour when accessing class methods. I think this must be done overloading the operator subsref. E.g. when I do:
classdef fooClass
methods
function [varargout] = subsref(A,S)
<Debug point>
% Some stuff
...
end
end
methods (Static)
function out = fooFunStatic( in )
...
end
end
if I define an instance of the class
instace = fooClass;
instance.fooFunStatic( ... )
I can see how the overloaded subsref is accessed. However, when I call the static method directly through the class name
fooClass.fooFunStatic( ... )
the overloaded subsref method is not accessed. I'm trying to modify the behaviour in this second case, when the .( ) caller is invoked directly from the class name. How could this be done? I would appreciate any idea.
Thank you in advance!

Answers (1)

Matt J
Matt J on 31 Jul 2015
Edited: Matt J on 31 Jul 2015
I don't think there is a way. However, it can be deduced from what you are saying that nothing being done by your subsref(A,S) actually makes use of the object A. I deduce this because you are expecting the same response from the call fooClass.fooFunStatic when no object is present.
Since subsref(A,S) is not doing anything with the object A, you should be able to relocate the code "Some stuff" to fooFunStatic where it will always be reached, no matter how the static method is called.
In other words, there should be no reason to do what you are trying to do...
  2 Comments
Jesus
Jesus on 3 Aug 2015
Well, I understand it may seem a little tricky, as this overloading was intended for a very specific purpose. I wanted to write a piece of code which applies within every called static method, not only within the one I added as an example.
But mainly, it surprises to me this different behaviour for the subsref method depending on wether a class instance or the class name are used to call the static method. I will leave the question open for a while just in case someone has anything else to contribute with.
Thank you for your answer!
Matt J
Matt J on 3 Aug 2015
But mainly, it surprises to me this different behaviour for the subsref method depending on wether a class instance or the class name are used to call the static method
Well, subsref is intended to be applied to an object. It is designed with the idea that the object has array-like behavior and that you want to customize the way it is indexed. Array-like behavior in a class-name is less foreseeable.
If you want the same piece of code called in all static methods, it should be just a little more typing to wrap that code in its own function and call it at the beginning of the methods.

Sign in to comment.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!