473,383 Members | 1,795 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,383 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 1804

"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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.