Class Objects Usage

Fred Schuld schuldwork at hotmail.com
Mon Jul 7 12:54:31 CDT 2008


I have compiled Octave from the latest 3.0.0+ source off HG with MSVC2005.
Michael G has helped (thanks) with many questions I had with getting a successful build with MSVC2005.

Anyways, I was interested in trying the support for class objects.

Entering "help class" returns:

 -- Built-in Function:  class (EXPR)
 -- Built-in Function:  class (S, ID)
      Return the class of the expression EXPR, as a string or create a
      class object from the structure S with name ID.



Using a simple example I create a file with filename "polynom.m" with the @file convention:  current directory\@polynom\polynom.m

Script code:

% Polynom script
function p = polynom(a)

% Polynom constructor

if nargin == 0
   p.c = [];
   p = class(p, 'polynom');
elseif isa(a,'polynom')
   p = a;
else
   p.c = a(:).';
   p = class(p, 'polynom');
end

If the polynom constructor is called with:  p = Polynom([1 0 3])


I get the following error:

error: class: invalid call from outside class constructor
error: evaluating assignment expression near line 11, column 6

If I use the "methods" octave command:  methods("polynom")


It returns:

Methods for class polynom:

polynom

Which is what I expect since it only has one method so far: the constructor.


I looked at the code starting at line 967 in ov-class.cc:


DEFUN (class, args, ,
  "-*- texinfo -*-\n\
@deftypefn {Built-in Function} {} class (@var{expr})\n\
@deftypefnx {Built-in Function} {} class (@var{s}, @var{id})\n\
\n\
Return the class of the expression @var{expr}, as a string or\n\

create a class object from the structure @var{s} with name @var{id}.\n\
@end deftypefn")
{
  octave_value retval;

  int nargin = args.length ();

  if (nargin == 1)
    retval = args(0).class_name ();

  else if (nargin == 2)
    {
      Octave_map m = args(0).map_value ();

      if (! error_state)
    {
      std::string id = args(1).string_value ();

      if (! error_state)
        {
          octave_function *fcn = octave_call_stack::caller ();


          if (fcn && fcn->is_class_constructor ())
        retval = octave_value (new octave_class (m, id));
          else
        error ("class: invalid call from outside class constructor");

...

I checked on the "is_class_constructor()" method and it is set to return FALSE on line 77 of ov-fcn.h:

virtual bool is_class_constructor (void) const { return false; }


Perhaps the "octave_class::in_class_method" would be better to call?

In summary it looks like the code is configured to return the output

"error: class: invalid call from outside class constructor" for all cases.


Is this expected operation or is there something wrong with my example script use of class objects?
_________________________________________________________________
Use video conversation to talk face-to-face with Windows Live Messenger.
http://www.windowslive.com/messenger/connect_your_way.html?ocid=TXT_TAGLM_WL_Refresh_messenger_video_072008
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080707/edefae26/attachment.html 


More information about the Octave-maintainers mailing list