dependent properties of parent objects - Why does this not work in getters?

  • Follow


Bonjour everyone,

I have a tree of objects that are supposed to have access to the same information by referencing properties (attributes) of their parent objects. All are subclasses of the same super class.
I tried to realize this by using getters of dependent properties:

class myClass
    properties
        parent
    end
    properties (Dependent = true)
         myProperty
    end
    methods
        function value = get.myProperty(this)
            value = this.parent.myProperty;
        end
    end
end

This however does not work. Matlab complains that 'the get method for dependent property 'myProperty' attempts to access the stored property value. Dependent property values don't store a value and can't be accessed from their get method.'

In my understanding the above construct should use the parent attribute and access the 'myProperty' attribute in the parent. Since 'myProperty' is dependent in parent as well (all parents have the same super class) it should call the get.myProperty method in the parent which in turn will call the parent parents get.myProperty method. I do not see where the 'attempt to access the stored property value' should take place outside the getter method.

As you might have guessed at some point some parent needs to provide the information of myProperty. I tried to do this by overriding the getter in sub classes, something MATLAB does not allow (and I don't see a good reason for this...).
My workaround to overriding is ugly but it (surprisingly) also solves the above problem: A regular method works just fine when called from the get.myProperty getter, although it executes the same code as the get.myProperty method above. I thus use two methods to do what a single one should be able to do:

class MyClass
.....
function value = get.myProperty(this)
    value = this.getMyProperty();
end
function value = getMyProperty(this)
    value = this.parent.myProperty;
end
....
end % MyClass

Can someone enlighten me why
(a) I cannot override getters in MATLAB
(b) my call to the parents property works in a regular method but not in the getter?

thank you for letting me use your braincells,
Marco
0
Reply Marco 8/18/2010 6:31:05 PM

"Marco Braun" <Braun.Marco@sca.uqam.ca> wrote in message <i4h8t9$isp$1@fred.mathworks.com>...
> Bonjour everyone,
> 
> I have a tree of objects that are supposed to have access to the same information by referencing properties (attributes) of their parent objects. All are subclasses of the same super class.
> I tried to realize this by using getters of dependent properties:
> 
> class myClass
>     properties
>         parent
>     end

Your description is unclear to me. You mean the propert myClass.parent is also an object of myClass. In any case, this thread discussed similar issues

http://www.mathworks.com/matlabcentral/newsreader/view_thread/276215#727061
0
Reply Matt 8/18/2010 7:49:04 PM


> Your description is unclear to me. You mean the propert myClass.parent is also an object of myClass. In any case, this thread discussed similar issues
> 
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/276215#727061

Yes, the property myClass.parent is also of type myClass. Thank you for the link to the other discussion.

Marco
0
Reply Marco 8/18/2010 9:17:19 PM

2 Replies
208 Views

(page loaded in 0.043 seconds)

Similiar Articles:








7/21/2012 9:01:13 PM


Reply: