[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Class Objects Usage
From: |
John W. Eaton |
Subject: |
Class Objects Usage |
Date: |
Tue, 08 Jul 2008 12:07:58 -0400 |
On 7-Jul-2008, Fred Schuld wrote:
|
| 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; }
That's the first definition of the virtual function in the base
class. Look again at the definition of this function in the
octave_user_function derived class.
| 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?
I don't know what is causing the problem for you. Here is what I see
with your function and a copy of Octave built from the current
sources:
octave:1> x = polynom
octave:2> class (x)
ans = polynom
octave:3> which polynom
which: `polynom' is the function from the file
/scratch/jwe/build/octave/@polynom/polynom.m
octave:4> type polynom
polynom is the function defined from the file
/scratch/jwe/build/octave/@polynom/polynom.m:
% 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
octave:5> methods ("polynom")
Methods for class polynom:
polynom
I'm using a Debian system. If this problem is specific to Windows,
then someone who uses Windows will have to help debug it.
jwe