help-octave
[Top][All Lists]
Advanced

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

Re: modify in place


From: Paul Kienzle
Subject: Re: modify in place
Date: Tue, 28 Nov 2000 10:52:17 +0000
User-agent: Mutt/1.2.5i

On Mon, Nov 27, 2000 at 07:18:23PM -0600, John W. Eaton wrote:
> On 27-Nov-2000, Ben Sapp <address@hidden> wrote:
> 
> | How can I modify a variable that is a new user defined type?  I want to
> | modify the actuall variable.  I do not want to make another modified
> | instance of the variable.  I tried the following piece of code and it
> | compiled fine but, ended up crashing on the line in
> | "non_const_function()" where I modify some internal variables in
> | myclass.  
> | 
> | ------------------------------------------------------------
> | octave_value &rep = const_cast<octave_value&>(args(0).get_rep());
> | octave_myclass &myclass = (octave_myclass &)(rep);
> | myclass.non_const_function();
> | -------------------------------------------------------------
> | 
> | I want it to work like this from the octave command line:
> | 
> | modify_in_place(a);
> | 
> | Now "modify_in_place" does not return any thing it just changes the
> | value of "a".  
> 
> You can't without changing a fundamental feature of Octave, which
> enforces pass by value semantics for function arguments.
> 
> jwe

You could if your type was a wrapper for a reference counted
representation.  Normal "out of place" modifications would check the
reference count and clone if not 1 while "in place" modifications
would not bother.  This will lead to nasty surprises, though, like:

        b = a;
        modify_in_place(a);
        if a == b, "surprise!", endif

unless you force the assignment operator for your type to clone the
representation.  Even then you may get some surprises if you combine
in place and out of place modifications in the same expression.  Be
warned that your values will be passed by reference to functions.  If
you want the usual by-value semantics, avoid in-place mods within the
function, or use your cloning assignment operator prior to updating.

Check things carefully!  And let me know how it goes: this may be a
way to manage widget handles if we ever get around to implementing a
Matlab-like GUI.  In this case, all modifications are in-place, and
we would want both b and a to point to the same repn after assignment, 
so cloning won't be necessary.

Paul Kienzle
address@hidden

> 
> 
> 
> -------------------------------------------------------------
> 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
> -------------------------------------------------------------
> 
> 



-------------------------------------------------------------
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
-------------------------------------------------------------



reply via email to

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