cloning objects

C

Cody Manix

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 ****ing ads]
 
T

Theo Yaung [Microsoft]

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" <[email protected]>
From: "Cody Manix" <[email protected]>
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 ****ing 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top