[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Octave-bug-tracker] [bug #49421] Can't set properties in a subclass aft
From: |
Mike Miller |
Subject: |
[Octave-bug-tracker] [bug #49421] Can't set properties in a subclass after chaining constructors in base class |
Date: |
Sat, 17 Dec 2016 20:33:49 -0000 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0 |
Update of bug #49421 (project octave):
Category: None => Interpreter
Item Group: None => Matlab Compatibility
Status: None => Need Info
Operating System: GNU/Linux => Any
Summary: Can't set properties in a subclass => Can't set
properties in a subclass after chaining constructors in base class
_______________________________________________________
Follow-up Comment #1:
I am able to reproduce this error with the following minimal example:
$ cat @base/base.m
classdef base < handle
properties
x;
y;
endproperties
methods
function self = base (a, b)
if (nargin == 0)
self.x = 0;
self.y = pi;
else
self = base ();
self.x = a;
self.y = b;
endif
endfunction
endmethods
endclassdef
$ cat @derived/derived.m
classdef derived < base
properties
z;
endproperties
methods
function self = derived (a = 0, b = pi, c = 42)
address@hidden (a, b);
self = builtin ("subsasgn", self, substruct (".", "z"), c);
endfunction
endmethods
endclassdef
The key part of this minimal example is that the base constructor overwrites
the "self" object with a new instance of type base, when it really should be a
derived object. Thus the derived constructor gets back a base object instead
of the one it originally started with, and it doesn't have the expected
properties.
What does Matlab do? Is there a correct way to do constructor chaining if this
is not the right way to do it? Instead of doing something like "self = base
()" should that be "address@hidden ()" instead? Should both work?
_______________________________________________________
Reply to this item at:
<http://savannah.gnu.org/bugs/?49421>
_______________________________________________
Message sent via/by Savannah
http://savannah.gnu.org/