octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Class Objects Usage


From: Fred Schuld
Subject: Class Objects Usage
Date: Mon, 7 Jul 2008 12:54:31 -0500

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 address@hidden

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. Get started.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]