How to pass a class name as argument to a function

15 次查看(过去 30 天)
Hello,
I'd like to initialize an element of a class with another class instance, and I'd like to pass in the particular class as an object. For example:
classdef Aclass < handle
properties
thingObj
end
methods
function this = Aclass(thingClassName)
this.thingObj = thingClassName(arg1,arg2,etc);
end
end
end
This code doesn't work, though ("Index exceeds matrix dimensions"). I assume the problem is you can't call the () operator on an existing object.
Instead, I do this, which does work, but isn't pretty:
classdef SomeClass < handle
properties
thingObj
end
methods
function this = SomeClass(thingClassName)
this.thingObj = eval([class(thingClassName) '(arg1,arg2,etc)']);
end
end
end
There's no checking in the string argument list, and errors in this line are hard to debug.
I could instantiate the object outside the constructor, but that's problematic. I'd like to instantiate the object within the "SomeClass" constructor because it knows the right way to do it.
I'm guessing this is really simple and that I haven't stumbled on the right way yet. Any ideas?
Thanks,
Charles

采纳的回答

Jeff Miller
Jeff Miller 2019-1-30
I think the first classdef ("Aclass") works fine, as long as you pass to its constructor the handle of the class you want instantiated. So, for example,
myobj = Aclass(@theClassToInstantiate)
  3 个评论
Paul Herselman
Paul Herselman 2024-2-20
This works great to just create a single instance of the class passed into the constructor, but I want to create an empty array of the class objects and that does not work:
function obj = clCircularBuffer(length, classToUse)
%CLCIRCULARBUFFER Construct an instance of this class
% must pass in the handle of the class to use, eg
% clCircularBuffer(100, @classToUse)
obj.bufferLength = length;
a = classToUse();
obj.buffer = classToUse.empty(length,0);
end
The line
a = classToUse();
works fine, but on the line
obj.buffer = classToUse.empty(length,0);
I get Dot indexing is not supported for variables of this type.
Steven Lord
Steven Lord 2024-2-20
If the class in question defines one or more of the array-creation functions you could call that array-creation function using the name of the class (or an instance of the class with the 'like' option). For example, the sym class from Symbolic Math Toolbox does define the zeros method as described on that documentation page:
z = zeros(3, 'sym')
z = 
z2 = zeros(4, 'like', z)
z2 = 
whos z z2
Name Size Bytes Class Attributes z 3x3 8 sym z2 4x4 8 sym
If it doesn't support those array-creation functions and you have to use the empty Static method, you could create a function handle to that method and call it.
name = "sym";
methodname = name + ".empty";
functionHandle = str2func(methodname);
z3 = functionHandle(3, 0)
z3 = Empty sym: 3-by-0
class(z3)
ans = 'sym'

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by