473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Copying Data Instead of Memory Addresses

I am currently trying to work on a program and I found it more convient to
have a tempory object which would hold a copy of data loaded from a file so I
wouldn't have to work with the overhead of loading from a file repeatedly.
My problem is that when I store the temporary object to another object,
changing the temporary object or its data also changes the data of the other
object.

Example:

MyClass a=new MyClass(2), b=new MyClass(3);
b=a;
a.data=10;
//b's data field now also equals 10 although what I really wanted to do was
//copy the integer data and keep it the same even if a's data field changed.

Since my objects are more complex than just one integer, I don't want to
have to manually move all the data and since there are also some objects
(such as Bitmaps) in the temporary object, so I would be unable to move the
data by hand without incurring the error described above.

Is there any way to create a copy of one object in such a way that the data
of the copy is not changed when the data of the original is changed?

Thank you.
Nov 17 '05 #1
3 1519
This is how it should be. Look at what you are doing.

Here create a pointer to a class and call that pointer "a" and create and
object that "a" now points to.
MyClass a=new MyClass(2),

Here create a pointer to a class and call that pointer "b" and create and
object that "b" now points to.
b=new MyClass(3);

now have b point to the same object a and forget all about the object b
pointed to.
b=a;
a.data=10;

You have to make a copy of "a" in order to do what you want.

MyClass a, b
a = new MyClass(2)
b = a.makecopy()

hope this helps
Chris
"Jack" <Ja**@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
I am currently trying to work on a program and I found it more convient to
have a tempory object which would hold a copy of data loaded from a file
so I
wouldn't have to work with the overhead of loading from a file repeatedly.
My problem is that when I store the temporary object to another object,
changing the temporary object or its data also changes the data of the
other
object.

Example:

MyClass a=new MyClass(2), b=new MyClass(3);
b=a;
a.data=10;
//b's data field now also equals 10 although what I really wanted to do
was
//copy the integer data and keep it the same even if a's data field
changed.

Since my objects are more complex than just one integer, I don't want to
have to manually move all the data and since there are also some objects
(such as Bitmaps) in the temporary object, so I would be unable to move
the
data by hand without incurring the error described above.

Is there any way to create a copy of one object in such a way that the
data
of the copy is not changed when the data of the original is changed?

Thank you.

Nov 17 '05 #2
You want your class to implement the ICloneable interface.

You can then copy the object by saying

MyClass b = a.Clone();

However, you do have to write Clone() yourself, and define what
"cloning" means for your class....

Nov 17 '05 #3
You want your class to implement the ICloneable interface.

You can then copy the object by saying

MyClass b = a.Clone();

However, you do have to write Clone() yourself, and define what
"cloning" means for your class....

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.