How to cast a subclass object to its superclass type..?

조회 수: 18 (최근 30일)
Bradley Stiritz
Bradley Stiritz 2011년 9월 10일
Hi everyone,
The MATLAB documentation states:
"MATLAB does not allow you to create arrays containing a mix of superclass and subclass objects because an array can be of only one class."
The MATLAB documentation for function cast() states:
"B = cast(A, newclass) converts A to class newclass, where newclass is the name of a built-in data type compatible with A."
Since we can't use the cast() function to convert a subclass to its superclass, how else can we perform the necessary conversion, in this or any general context..?
For example:
classdef MySuperClass
...
end
classdef MySubClass < MySuperClass
...
end
% Create subclass object:
mySubClassObj = MySubClass(..);
% Try to cast to superclass:
mySubClassObj = cast(mySubClassObj,'MySuperClass');
Error using cast
Unsupported class for conversion.
Any guidance / suggestions appreciated,
Thanks, Brad

채택된 답변

Daniel Shub
Daniel Shub 2011년 9월 14일
First, since r2011a MATLAB supports "Heterogeneous Arrays":
You can also essentially do what cast does by defining your own conversion method:
  댓글 수: 1
Bradley Stiritz
Bradley Stiritz 2011년 9월 14일
Hi Daniel, thanks very much for digging out these DOC pages for me, very appreciated :)
brad

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subclass Definition에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by