473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

making a copy of a list of pointers to objects

Hello,

i just wanted to ask what the best way would be to make a copy of a list of pointers to objects. The objects themselves each contain more pointers to other objects. All of these pointers should point to new objects in the copied list and not to the original objects.

I thought it might be good to write a copy constructor for the list but I'm not sure how I would do that. Do I have to use inheritance?

Any help would be appreciated.

Thanks.
Mar 26 '07 #1
3 2982
Ganon11
3,652 Expert 2GB
You should not need inheritance.

What you are saying is you have 1 linked list (list1) containing pointers to objects. You want to create a new list (list2) containing pointers to objects. Each object of list2 should have the same properties as the objects of list1, but they should be separate objects - as well, the pointers of list2 will be pointing to different locations then list1.

You will need to access a node of list1. You will have access to its data (the pointer) and the next node. You should get the object that the data points to, create a pointer to that object type and initialize it to a new object (passing the list1 object as a parameter), then add the new pointer to list2. Then you can repeat on the next node.

You will have to write your own copy constructor for the class.
Mar 26 '07 #2
Thanks Ganon11 for the prompt reply. Your description of what I want to do is exactly right yes. And I can see how your proposed solution would work. I just thought it would be nice to be able to say something like list<ObjectType *> list2 = getCopyOfList(); and then getCopyOfList() has at the end return list1; and then by using copy constructors, it would automatically make a complete new copy of the list.

It would look really clean and if I'm not mistaken, it would also mean that when the function from which I call list< ObjectType *> list2 = getCopyOfList() exits, the destructor of list2 would automatically be called and I could make it delete all the objects being pointed at in the list by overloading the destructor of the list... I don't know if that is clear enough, it seems a bit complex :o)
Mar 26 '07 #3
Ganon11
3,652 Expert 2GB
That's clear, but from a short description like that, it sounds like getCopyOfList would create a shallow copy. That is, the new list you create would have new pointers, but these pointers would point to the same objects as in list1. This is why you have to be especially careful when copying pointers - you don't want to end up with 2 pointers pointing to 1 object.
Mar 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
10
by: spoc | last post by:
I have been using memcpy to copy one class to another of the same type. There are reasons why I had to do this bug am getting some odd crashes and maybe I'm doing something dodgy copying classes...
14
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
35
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
5
by: jgscott | last post by:
I've been trawling around for an answer to this question and thought I'd try here. I have a class Graph, which has a std::list<Nodeas a class member. Node it itself a class that makes extensive...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.