Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Inferring class hierarchy from within static method
Replies: 4   Last Post: Oct 10, 2012 4:55 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 93
Registered: 12/6/04
Inferring class hierarchy from within static method
Posted: Oct 8, 2012 3:58 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hello all,

I've run into this problem a couple of times in the past few years trying to put various frameworks together using Matlab OO. One was an extension of the popular "xunit" framework on the File Exchange, and another is a framework to support save/load of object and nested trees to XML. The problem arises in that I want to define a static method in a base class to support some basic functionality, but it needs to know something about the subclass that originally created the function call.

There seems to be no way of doing this out of the box. Consider a base class called "Shape" and a subclass "Square". Consider "Shape" defines a static method "LoadFromFile". In this scenario, you could call "LoadFromFile" using:
However, within LoadFromFile, there is no way of knowing that this was called using the "Square" class.

An example of a use case for this is the XML save/load example. Consider the Shape save/load method. It is possble to create a generic load capability that can create a Shape object from a file. That file presumably knows what kind of shape it is. The static method "LoadFromFile" can then possibly create many different kind of Shapes, however if we called the method from "Square" class, we would want to make sure that we loaded a Square, and throw an error if the file happened to be for a Circle. Sure, I could overload LoadFromFile() within Square to accomplish this, but that type of thing could be handled in one place without duplicating code in every shape subclass if it was possible to infer the calling classname from within LoadFromFile.

I've gotten around this somewhat by requiring that subclasses overload the static method, then explicitly call the superclass static method. In this case, the superclass can query "dbstack" to determine the calling subclass. However this seems rather kludgy and fragile. Was wondering if anyone else has tackled this problem, or if there are any plans on adding this kind of capability.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum 1994-2015. All Rights Reserved.