473,434 Members | 1,638 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,434 software developers and data experts.

cloning objects

iam trying to program a copy constructor that is able to create a bitwise
copy of its argument:

MyClass
{
int a;
// lots of different fields goes here
float x;
MyClass (MyClass my)
{
// create an exact copy of mc here
}
}

normaly, object.MemberWiseClone() would be the way to go but it seems its
useless within a constructor.
is there another way (without copying every item manually)?

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]

Nov 15 '05 #1
1 9136
Hi Cody --

Doing so might cause some tricky behavior for a class. If you think about
classes being able to be inherited, it's possible that the instance passed
into the "copy constructor" is actually a subclass of that type, with
different fields, so one can't just always make a direct copy. For
classes, I'm pretty sure that the only option is to write a programmatic
copy.

It's certainly possible to make a quick copy if your data structure is a C#
struct. Structs have certain constraints, such as they can't be extended
by other classes or structs. In addition, they're pass by value, so making
a copy is semantically inherent. To make a copy constructor on a struct,
you can do it like this:

struct MyStruct {
int a;
// lots of different fields goes here
float x;
MyStruct (MyStruct my) {
// create an exact copy of mc here
this = my;
}
}

In struct constructors, the keyword "this" has the semantic similar to an
"out parameter" (if you're familiar with reference/output parameters in
C#). This is documented in the C# specification.

If you're bent on copying all the fields in a class though, you can do it
via Reflection.

using System.Reflection;

class MyClass {
int a;
// lots of different fields goes here
float x;

MyClass() {
// default constructor
}

MyClass (MyClass my) {
// create an exact copy of mc here
FieldInfo[] fields = typeof(MyClass).GetFields(BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance);
foreach(FieldInfo f in fields) {
f.SetValue(this,f.GetValue(oth));
}
}
}

Something like the above should work out for you, if not feel free to reply
to this thread on the newsgroup.

I hope this helps,

--Theo
Theo Yaung
Visual Studio .NET
Microsoft Corp.
--------------------
Reply-To: "Cody Manix" <de********@web.de>
From: "Cody Manix" <de********@web.de>
Subject: cloning objects
Date: Mon, 18 Aug 2003 14:44:28 +0200

iam trying to program a copy constructor that is able to create a bitwise
copy of its argument:

MyClass
{
int a;
// lots of different fields goes here
float x;
MyClass (MyClass my)
{
// create an exact copy of mc here
}
}

normaly, object.MemberWiseClone() would be the way to go but it seems its
useless within a constructor.
is there another way (without copying every item manually)?

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 15 '05 #2

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

Similar topics

4
by: Tom | last post by:
Hey ho, The release of PHP5 seemed like a good reason to try to learn it once more... I'm reading through the O'Reilly PHP/MySQL book and right now, I'm fiddling with objects. I just...
11
by: K.K. | last post by:
Suppose I have a class called Vehicle with many fields. Now I make a new class derived from Vehicle called Car. I'd like to make a method to copy the data from a Vehicle instance to a Car instance....
8
by: Tom | last post by:
I've a problem. I want to clone an object having a list of other objects (and so on :/). Do you know any other way than ICloneable.Clone() implementation for all classes in the way? Help..
6
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete...
1
by: illegal.prime | last post by:
I would like to have an array of objects (whose class I define) and then just invoke either: MyClass clonedArray = (MyClass) myArray.Clone(); OR Array.Copy(myArray, clonedArray, myArray.Length);...
10
by: pazabo | last post by:
Hi, I'm trying to somehow implement polymorphic object cloning (just as it is in Java), but when I write: class Object { public: virtual Object * clone() const = 0; // ... }
1
by: colmkav | last post by:
Hi, what is the main reason why cloning is done to tables in Access VBA code? regards Colm
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
9
gits
by: gits | last post by:
This short article introduces a method that may be used to create a 'deep-copy' of an javascript object. You might ask: 'Wherefore do we need this?' ... Answer: 'Only variable-values of the basic...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
1
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,...
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...

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.