Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Problem with references

 
Thread Tools Search this Thread
Old 11-03-2009, 10:06 AM   #1
Default Problem with references


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
  Reply With Quote
Old 11-03-2009, 01:40 PM   #2
Jeff Flinn
 
Posts: n/a
Default Re: Problem with references
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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46