help-octave
[Top][All Lists]
Advanced

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

Re: oct-files: assigning to substructures? assigning to multidimensional


From: Olaf Till
Subject: Re: oct-files: assigning to substructures? assigning to multidimensional structures?
Date: Sat, 28 Jun 2008 23:08:11 +0200
User-agent: Mutt/1.5.13 (2006-08-11)

On Sat, Jun 28, 2008 at 06:35:54PM +0200, Jaroslav Hajek wrote:
> ...
> The problem is caused by the general Octave's philosophy of (nearly)
> everything passed by value (so that you never have a problem of
> dangling references), with wrappers for the actual reference-counted
> objects, so that a read-only copy is as cheap as a reference.
> Octave_map is no exception, and now the problem is not hard to guess:
> st.contents("b1")(0).map_value() creates a *temporary copy* of the
> Octave_map object inside, you then assign to this map (upon which a
> clone of the original object is made),
> and then it just dies away as the statement ends.
> 
> So a proper way would be (similarly to how you used the whole function):
> st.contents("b1")(0) = st.contents("b1")(0).map_value().assign("c2", tmp);
> 
> To save typing (and evaluating, probably) st.contents("b1") twice, you can 
> use:
> {
>   Cell& c = st.contents("b1");
>   c(0) = c(0).map_value().assign("c2", tmp);
> }

Ah, thanks a lot. Of course, .map_value() makes a copy... Did not
think of assigning with "=" to .contents("..")(..). So .contents()()
seems to return a reference... this will probably also solve the
second problem mentioned in my post.

I'm very impressed that it's possible to get help with such a problem
within 1 h at saturday evening :-) , thanks again.

Olaf

> typicaly, only the methods or operators designed for indexed
> assignment will give you a proper reference, usually you get a (cheap)
> copy.
> 
> Alternatively, you can use the octave_value subsasgn method (but as
> that involves constructing a std::list of octave_idx_list objects, I
> doubt you would find that more convenient).
> 


reply via email to

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