[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
problem with subsasgn
From: |
abarth |
Subject: |
problem with subsasgn |
Date: |
Sat, 29 Oct 2005 14:16:46 -0500 |
User-agent: |
SquirrelMail/1.4.5 [CVS] |
Hi,
I have a problem with the subsasgn method for a self-written octave object
(the octcdf toolbox). This octave object is based on the box.cpp example
of Muthiah Annamalai. I have copied a minimal example below to reproduce
this problem. The problematic code is the following:
b = BoxMake(); % create a box object
b(1) = 1; % invoke subasgn
typeinfo(b) % check type of b
In a script everything works well:
octave:1> box_script
subsasgn
ans = Box
So after the assignment the type of b is Box. But if the code is placed in
a function, I get:
octave:1> box_fun
destructor
ans = scalar
The destructor of b is called and after the assignment b is a scalar. Why
is that? I use octave version 2.1.71 (i686-pc-linux-gnu). Below is the
source code. Any help is greatly appreciated.
Cheers
Alex
Just past the following in a shell:
cat > BoxMake.cpp <<EOF
#include<iostream>
#include<octave/config.h>
#include<octave/oct.h>
#include<octave/parse.h>
#include<octave/dynamic-ld.h>
#include<octave/oct-map.h>
#include<octave/oct-stream.h>
#include <octave/ov-base.h>
#include<octave/ov-base-scalar.h>
#include<vector>
#include<string>
using namespace std;
class Box: public octave_base_value
{
public:
Box():octave_base_value() { }
Box(octave_value box_val):octave_base_value() {}
octave_value subsasgn(const std::string & type,
const std::list < octave_value_list > &idx,
const octave_value & rhs) {
octave_stdout << "subsasgn" << endl;
octave_value retval = octave_value(this, count + 1);
return retval;
}
~Box() {
octave_stdout << "destructor" << endl;
}
void print (std::ostream& os, bool pr_as_read_syntax = false) const {
os<<" Box " << endl;
}
size_t byte_size (void) const { return 100; };
bool is_constant () const { return true; }
bool is_defined () const { return true; }
bool is_map () const { return true; }
private:
DECLARE_OCTAVE_ALLOCATOR
DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
};
DEFINE_OCTAVE_ALLOCATOR (Box);
DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (Box, "Box","Box");
DEFUN_DLD(BoxMake,args,,"BoxMake()\n\
makes new instances of Box objects\
to fill into the world of octave")
{
static bool type_loaded = false;
if (! type_loaded)
{
Box::register_type ();
mlock ("BoxMake");
}
Box *b=new Box();
return octave_value(b);
}
EOF
mkoctfile BoxMake.cpp
cat > box_script.m <<EOF
b = BoxMake();
b(1) = 1;
typeinfo(b)
EOF
cat > box_fun.m <<EOF
function box_fun
b = BoxMake();
b(1) = 1;
typeinfo(b)
EOF
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- problem with subsasgn,
abarth <=