Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Constructor call to superclass not a top-level statement

26 views
Skip to first unread message

Pravit C

unread,
Jul 19, 2009, 8:38:01 PM7/19/09
to
Recently got this error message in r2008a when running some code that had been written in r2007a.

I have a class A with members a,b,c, and a class B which inherits from class A. Class B has members d,e,f as well.

Class B has a variable argument constructor; you can pass it another class B and it attempts to initialize a,b,c like so:

B = B@A( varargin{1} );

Alternately, you can specify members a-f explicitly, in which case:

B = B@A( varargin{1:3} );

(A's constructor is written to handle both as well).

However, when I run my code, I get the error message that you can only call the superclass constructor from the top-level (I suppose not in the if statement). Is there some other way to get this functionality?

Pravit C

unread,
Jul 19, 2009, 9:28:01 PM7/19/09
to
Answered my own question, see "No conditional calls to superclass constructors" in the Matlab documentation here: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_oop/brd2m9e-1.html

My revised constructor for class B looks like this:

if nargin == 1
bInstance = varargin{1};
superclass_args = { bInstance };
elseif nargin == 3
superclass_args = varargin{1:3}
end

newB = B@A( superclass_args{:} );

if nargin == 1
(assign d,e,f from bInstance)
if nargin == 3
(assign d,e,f from varargin)
end

A bit convoluted, but at least it works.

0 new messages