From: NL on
Hi,

A simple question, I'm sure, but I can't seem to find the answer. How
do modify the contents of a container in a for each construct? That
is, obtain a reference to the object in the container rather than a
temporary copy? (The code below _won't_ modify the "FooVector")

struct Foo {
int a;
}
vector<Foo> FooVector;

for each( Foo f in FooVector )
{
f.a = some_new_value;
}

Thanks,
NL
From: mzdude on
On Feb 3, 4:07 pm, NL <norv...(a)gmail.com> wrote:
> Hi,
>
> A simple question, I'm sure, but I can't seem to find the answer. How
> do modify the contents of a container in a for each construct? That
> is, obtain a reference to the object in the container rather than a
> temporary copy? (The code below _won't_ modify the "FooVector")
>
> struct Foo {
>  int a;}
>
> vector<Foo> FooVector;
>
> for each( Foo f in FooVector )
> {
>   f.a = some_new_value;
>
> }
>

try
for each(Foo &f in FooVector)
From: NL on
On Feb 3, 1:25 pm, mzdude <jsa...(a)cox.net> wrote:
> On Feb 3, 4:07 pm, NL <norv...(a)gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > A simple question, I'm sure, but I can't seem to find the answer. How
> > do modify the contents of a container in a for each construct? That
> > is, obtain a reference to the object in the container rather than a
> > temporary copy? (The code below _won't_ modify the "FooVector")
>
> > struct Foo {
> >  int a;}
>
> > vector<Foo> FooVector;
>
> > for each( Foo f in FooVector )
> > {
> >   f.a = some_new_value;
>
> > }
>
> try
> for each(Foo &f in FooVector)

I tried that, it gives an error:

error C2440: 'static_cast' : cannot convert from 'const Foo' to 'Foo
&'


From: mzdude on
On Feb 3, 4:32 pm, NL <norv...(a)gmail.com> wrote:
> On Feb 3, 1:25 pm, mzdude <jsa...(a)cox.net> wrote:
>
>
>
>
>
> > On Feb 3, 4:07 pm, NL <norv...(a)gmail.com> wrote:
>
> > > Hi,
>
> > > A simple question, I'm sure, but I can't seem to find the answer. How
> > > do modify the contents of a container in a for each construct? That
> > > is, obtain a reference to the object in the container rather than a
> > > temporary copy? (The code below _won't_ modify the "FooVector")
>
> > > struct Foo {
> > >  int a;}
>
> > > vector<Foo> FooVector;
>
> > > for each( Foo f in FooVector )
> > > {
> > >   f.a = some_new_value;
>
> > > }
>
> > try
> > for each(Foo &f in FooVector)
>
> I tried that, it gives an error:
>
>         error C2440: 'static_cast' : cannot convert from 'const Foo' to 'Foo
> &'- Hide quoted text -
>
Sorry. I use the boost library and the code would look like

BOOST_FOREACH( Foo &f, FooVector )

so I thought it would be worth a shot.
From: NL on
Hi,

> > > try
> > > for each(Foo &f in FooVector)
>
> > I tried that, it gives an error:
>
> >         error C2440: 'static_cast' : cannot convert from 'const Foo' to 'Foo
> > &'- Hide quoted text -
>
> Sorry. I use the boost library and the code would look like
>
> BOOST_FOREACH( Foo &f, FooVector )
>

I guess I'll switch over to the boost macro, since I'm already using
other boost stuff in the project. I was leaning toward the MS version
of for,each,in since it looks more elegant, but I guess it's limited
to "read-only".
Also the lack of thorough documentation on it leads me to think it may
be somewhat temporary.

Thanks,
Norvin