How do I do a pointer to a class function?
23 views (last 30 days)
Show older comments
I have this code snippet:
N.phyClass = phy_constructor(cfg);
N.txClass = tx_constructor(cfg,N.phyClass);
N.macClass = mac_constructor(cfg,N.phyClass);
Both txClass and macClass have the member function txStep. I want to do a function pointer to that function.... but not sure if I can, unsuccessful so far. How do I point to a class method? I want to do something like
N.txStep = @N.txClass.txStep, or N.txStep = @ N.macClass.txStep
in this way, later code will just grab the right version of txStep. Please advise?
Thanks, Tom
0 Comments
Answers (3)
Steven Lord
on 19 Feb 2016
Just use N.txStep = @txStep. When you call it, MATLAB will determine the appropriate class's method to call.
N.txStep(N.txClass) % Calls the txClass method
N.txStep(N.macClass) % Calls the macClass method
It can get a little trickier when the function can accept multiple inputs and you pass in instances of multiple classes as inputs. In that case you may need to establish a precedence ordering of the two classes. See this documentation page and this one for more information.
0 Comments
per isakson
on 19 Feb 2016
Edited: per isakson
on 19 Feb 2016
Matlab doesn't have C-languge-pointers.
This example might ...
a = A;
b = B;
m = M(a);
m.step
m = M(b);
m.step
outputs
step of A running
step of B running
>>
where
classdef M < handle
properties
stepper
end
methods
function this = M( obj )
this.stepper = obj;
end
function step( this )
this.stepper.step
end
end
end
and
classdef A < handle
methods
function step( this ) %#ok<MANU>
disp( 'step of A running' )
end
end
end
and
classdef B < handle
methods
function step( this ) %#ok<MANU>
disp( 'step of B running' )
end
end
end
0 Comments
Patricio
on 23 Nov 2019
Edited: Patricio
on 23 Nov 2019
There is a sneaky Matlab way to do this that exploits functions within fuctions and the fact that the internal function will have access to the scope of the external one. The member function would be as follows:
function funcPtr = entryPoint(this)
funcPtr = @redirectFunction;
function redirectFunction()
this.actualfunctionToCall();
end
end
then in the Matlab code, you could do as follows:
myFunc = classObj.entryPoint();
which would give you the function pointer to the internal function. Execution
myFunc();
would redirect to the actual member function. It is possible to include arguments in the redirect function and they'd be accessible to you in the internal function. You could also pass in extra arguments to the entryPoint function and those arguments would be accessible to the internal function (presuming that inernal function were defined to accept arguments, varagin is possible too).
Mind you, this will be permanently tied to the actual instance classObj. If you wanted the function pointers to many instances, then you'd have to invoke the entryPoint for each instance, then maintain the function handles in separate variables or in an array or list, to iterate through them or do other stuff.
In your case, the call would be
N.txStep = N.txClass.entryPoint();
... code ...
N.txStep();
It would be easy to then overwrite the txStep field of N to point to whichever you wanted.
I haven't figured out a better way and use this approach all the time. I even have static member functions for a class do this to have run-time dynamically instantiated class objects based on the arguments and externally available (in the outer function scope) variables. It's a cheap way to have a dynamic factory interface.
If I don't think of a way to do otherwise, or Matlab doesn't provide a solution like can be done in python, this is how I am going to implement a ROS message callback within Matlab that is object specific.
1 Comment
Patricio
on 23 Nov 2019
EDIT:: Seems like my answer is an alternative version of the first answer. The first answer creates a new class to manage the same thing without requiring class-based entry points. Thinking further, it is possible then, to just create a function within a function that does the same and that would be the simplest approach.
function funcPtr = entryPoint(theObj)
funcPtr = @redirectFunction;
function redirectFunction(varargin)
theObj.actualfunctionToCall(varargin{:});
end
end
Execution would be:
N.txStep = entryPoint(N.txClass);
... code ...
N.txStep();
and you'd have the same effect. Uses varargin for the redirectFunction to permit arguments. Should work.
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!