Prev: "PORTING C" > How to assign a value to a define statement?
Next: Using pointer-to-member of incomplete type in constructor call in VS2008
From: NL on 3 Feb 2010 16:07 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 3 Feb 2010 16:25 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 3 Feb 2010 16:32 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 3 Feb 2010 16:48 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 4 Feb 2010 12:59
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 |