473,324 Members | 2,196 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,324 software developers and data experts.

Shallow copies of objects - or - Copy constructors

So I have a Product and an extending class ProductLine. I want to create a
new ProductLine from a Product.

I wrote this in a constructor:

public Product(Product p)
{
foreach (System.Reflection.FieldInfo info in
p.GetType().GetFields(System.Reflection.BindingFla gs.Instance |
System.Reflection.BindingFlags.NonPublic))
info.SetValue(this, info.GetValue(p));
}

First, is this a huge performance hit? I've heard rumors about reflection
being the devil for performance, but as I understand it, reflection is a
central technology for the framework, and therefore there is lots of it
going on, so why would a bit more hurt anything?

Second, did I solve a problem that has already been solved? I know there is
a Memberwise clone, but this is invalid (assigning to this):

public Product(Product p)
{
this = (Product)p.MemberwiseClone();
}

Also, ICloneable's Clone method has a similar downfall.

Any feedback would be great. I've posted on this before with no response, so
I'd like to see even a "Yeah, I think that works" type of response.

Thanks everybody.

Adam
Nov 16 '05 #1
2 1803

"Adam W Root" <ad**@canright.com> wrote in message
news:uB*************@TK2MSFTNGP10.phx.gbl...
So I have a Product and an extending class ProductLine. I want to create a
new ProductLine from a Product.

I wrote this in a constructor:

public Product(Product p)
{
foreach (System.Reflection.FieldInfo info in
p.GetType().GetFields(System.Reflection.BindingFla gs.Instance |
System.Reflection.BindingFlags.NonPublic))
info.SetValue(this, info.GetValue(p));
}

First, is this a huge performance hit? I've heard rumors about reflection
being the devil for performance, but as I understand it, reflection is a
central technology for the framework, and therefore there is lots of it
going on, so why would a bit more hurt anything?

Second, did I solve a problem that has already been solved? I know there
is
a Memberwise clone, but this is invalid (assigning to this):
Yes, this is the point of Memberwise Clone. I would recommend *not* using
copy constructors and using ICloneable for various reasons, not the least of
which polymorphic behaviour. Copy constructors require you to know the
*exact* type of the object when you intend to clone it.
public Product(Product p)
{
this = (Product)p.MemberwiseClone();
}

Also, ICloneable's Clone method has a similar downfall.

The proper way to do things would be something like:

public object Clone()
{
MyObject x = this.MemberwiseClone();
return x;
}

while doing any particulars you need to do to x to handle other
cloning(references type fields are shallow copied(only the reference is
copied) in MemberwiseClone()). Any feedback would be great. I've posted on this before with no response,
so
I'd like to see even a "Yeah, I think that works" type of response.

Thanks everybody.

Adam

Nov 16 '05 #2

"Adam W Root" <ad**@canright.com> wrote in message
news:uB*************@TK2MSFTNGP10.phx.gbl...
So I have a Product and an extending class ProductLine. I want to create a
new ProductLine from a Product.

I wrote this in a constructor:

public Product(Product p)
{
foreach (System.Reflection.FieldInfo info in
p.GetType().GetFields(System.Reflection.BindingFla gs.Instance |
System.Reflection.BindingFlags.NonPublic))
info.SetValue(this, info.GetValue(p));
}

First, is this a huge performance hit? I've heard rumors about reflection
being the devil for performance, but as I understand it, reflection is a
central technology for the framework, and therefore there is lots of it
going on, so why would a bit more hurt anything?

Second, did I solve a problem that has already been solved? I know there
is
a Memberwise clone, but this is invalid (assigning to this):
Yes, this is the point of Memberwise Clone. I would recommend *not* using
copy constructors and using ICloneable for various reasons, not the least of
which polymorphic behaviour. Copy constructors require you to know the
*exact* type of the object when you intend to clone it.
public Product(Product p)
{
this = (Product)p.MemberwiseClone();
}

Also, ICloneable's Clone method has a similar downfall.

The proper way to do things would be something like:

public object Clone()
{
MyObject x = this.MemberwiseClone();
return x;
}

while doing any particulars you need to do to x to handle other
cloning(references type fields are shallow copied(only the reference is
copied) in MemberwiseClone()). Any feedback would be great. I've posted on this before with no response,
so
I'd like to see even a "Yeah, I think that works" type of response.

Thanks everybody.

Adam

Nov 16 '05 #3

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

Similar topics

8
by: dan | last post by:
without stirring the pot too much -- could someone please point me to whatever documentation exists on the philosophy, semantics, and practical implications of how Python implements the...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
1
by: Adam W Root | last post by:
So I have a Product and an extending class ProductLine. I want to create a new ProductLine from a Product. I wrote this in a constructor: public Product(Product p) { foreach...
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
8
by: coredumperror | last post by:
Hi all you C++ gurus out there, I've got what may be an unusual question. What's the best way to get a shallow copy of a class that you've programmed to prevent accidental shallow copying? I've...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
5
by: pauldepstein | last post by:
I recently had a job interview question which I totally failed. (The question seemed excellent from an objective point of view, but having completely failed to do it, my subjective feelings are...
4
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.