C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 17th, 2004, 10:34 PM
darkstar darkstar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 darkstar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
shared_ptr Deep Copy

How can I get copy of a container which has shared pointers. In the following exmaple I would like to get a copy of vecIntPtr vector so that any changes that I make in the copy, do not affect values pointed by the pointers in vecIntPtr. I tried doing that by creating vecIntPtrTemp but it did not work.

Thanks in advance!

#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>

int main()
{
typedef boost::shared_ptr<int> intPtr;
std::vector<intPtr> vecIntPtr;

intPtr x(new int(10));
vecIntPtr.push_back(x);
std::cout << *vecIntPtr[0] << std::endl;

std::vector<intPtr> vecIntPtrTemp(vecIntPtr);
*vecIntPtrTemp[0] = 100;

std::cout << *vecIntPtrTemp[0] << std::endl;
std::cout << *vecIntPtr[0] << std::endl;

return 0;
}

Output:

10
100
100

Reply With Quote
  #2  
Old July 18th, 2004, 06:13 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
When you create a pointer all you are doing is creating a reference to a piece of memory. When you copy that pointer you get a variable that also references that same piece of memory. So when you change what either one points to you still change the same original value. To avoid affecting the original value you need to create a new variable of that type not just a pointer to it and change the value of that.

-KM-

Reply With Quote
  #3  
Old July 18th, 2004, 07:48 AM
darkstar darkstar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 darkstar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
container of pointers

In this case its easy to copy the values in int pointer and create a new vector.....My problem is that
I have vector of a object which is very big and which has another object insid it so I was wondering if there
is any smart pointer which could let me have the speed of container of pointers and give me the ability to get a
new copy of container of pointers when I need one for some specific algorithm where I need to backtrack if I am not
satisfied with the new changes.

Reply With Quote
  #4  
Old July 20th, 2004, 05:14 PM
Sharon White Sharon White is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Location: London
Posts: 4 Sharon White User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I think you probably need to write the smart container yourself if speed is the issue.

Creating dynamic objects is typically more costly in terms of the memory allocation than the operations performed on the object.

I would write templates that contains duplicates members for back tracking.

_____________________________________________________
Sharon White

Reply With Quote
  #5  
Old July 28th, 2004, 12:38 PM
darkstar darkstar is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 darkstar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks Sharon,

I think duplicate members will work out fine for me .......... creating a smart container is something which I don't want to attempt right now due to my limited knowledge of templates.........


Quote:
Originally Posted by Sharon White
I think you probably need to write the smart container yourself if speed is the issue.

Creating dynamic objects is typically more costly in terms of the memory allocation than the operations performed on the object.

I would write templates that contains duplicates members for back tracking.

_____________________________________________________
Sharon White

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > shared_ptr Deep Copy


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT