![]() |
|
|
|
#1 |
|
In a function 'update()' I read and update a std::vector<MyObj> located in
another object 'Test'. Now I would like these changes to be visible after 'update()' returns (both for the container and the elements inside). The function I use to read the std::vector<MyObj> therefore returns a reference to the container: std::vector<MyObj> & Test::getContainer() { return m_container; } Here is the implementation of 'update()' void update(){ std::vector<MyObj> container = test.getContainer(); int size = container.size(); for (int i=0; i<size; i++) { // Changes to objects in the container should be visible outside this function. MyObj & mo = container[i]; mo.setId(i); } // Below should be unnecessary test->setContainer(container); } But the changes are not visible after 'update()' returns unless I add the call: test->setContainer(container); after the above loop. But is the point of using references not that this kind of explicit updating is unncessary? carl |
|
|
|
|
#2 |
|
Posts: n/a
|
Daniel T. wrote:
> "carl" <carl@.com> wrote: > >> In a function 'update()' I read and update a std::vector<MyObj> located in >> another object 'Test'. >> >> Now I would like these changes to be visible after 'update()' returns (both >> for the container and the elements inside). >> >> The function I use to read the std::vector<MyObj> therefore returns a >> reference to the container: >> >> std::vector<MyObj> & Test::getContainer() >> { >> return m_container; >> } >> >> Here is the implementation of 'update()' >> >> void update(){ >> >> std::vector<MyObj> container = test.getContainer(); >> int size = container.size(); >> for (int i=0; i<size; i++) { >> >> // Changes to objects in the container should be visible outside this >> function. >> MyObj & mo = container[i]; >> mo.setId(i); >> } >> >> // Below should be unnecessary >> test->setContainer(container); >> } >> >> But the changes are not visible after 'update()' returns unless I add the >> call: >> >> test->setContainer(container); >> >> after the above loop. But is the point of using references not that this >> kind of explicit updating is unncessary? > > 'container' isn't a reference, only the return value of getContainer() > is. Oddly, you made 'mo' a reference so you seem to understand the > principle. > > Frankly, if you are going to make getContainer() return a reference > anyway, why not just make 'm_container' public? Or better yet make it Test::update, and have it the behavior encapsulated. Jeff Jeff Flinn |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Internet Firewall FAQ in Dutch | William L. Sun | Computer Security | 21 | 02-14-2005 03:11 PM |
| Ron Chiplin Allied Bakeries Cardiff. Project | ronald.chiplin | Computer Support | 11 | 06-10-2004 02:12 AM |
| Freezing Problem - Memory | Vincent Wonnacott | Computer Support | 1 | 05-19-2004 05:35 AM |
| Re: Serious Computer Problem | hootnholler | A+ Certification | 1 | 11-24-2003 12:18 PM |
| Re: Serious Computer Problem | Bret | A+ Certification | 0 | 11-19-2003 12:51 AM |