473,799 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Reflect ion.FieldInfo info in
p.GetType().Get Fields(System.R eflection.Bindi ngFlags.Instanc e |
System.Reflecti on.BindingFlags .NonPublic))
info.SetValue(t his, 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.Memb erwiseClone();
}

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 1826

"Adam W Root" <ad**@canright. com> wrote in message
news:uB******** *****@TK2MSFTNG P10.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.Reflect ion.FieldInfo info in
p.GetType().Get Fields(System.R eflection.Bindi ngFlags.Instanc e |
System.Reflecti on.BindingFlags .NonPublic))
info.SetValue(t his, 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.Memb erwiseClone();
}

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.Memberwise Clone();
return x;
}

while doing any particulars you need to do to x to handle other
cloning(referen ces 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******** *****@TK2MSFTNG P10.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.Reflect ion.FieldInfo info in
p.GetType().Get Fields(System.R eflection.Bindi ngFlags.Instanc e |
System.Reflecti on.BindingFlags .NonPublic))
info.SetValue(t his, 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.Memb erwiseClone();
}

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.Memberwise Clone();
return x;
}

while doing any particulars you need to do to x to handle other
cloning(referen ces 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
4535
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 assignment operator? So far I can't find much useful in the regular documentation. I understand the basic concept that names are bound to objects which exist on the heap, but that still doesn't explain why a = b doesn't _always_ cause a to point to the...
5
3289
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 assignment Student* john = michael; both john and michael share the same object. This type of an assignment is different then value-assignmnet semantics used by class variables, as in
2
12354
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
229
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 (System.Reflection.FieldInfo info in p.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic))
2
10121
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: http://blogs.msdn.com/brada/archive/2004/05/03/125427.aspx This artivle seems to hint that I should not use System.IClonable but instead define my own interface(s) for cloning. Now since this article is rather old and since they did not obsolete IClonable there might be a new "best practise".
8
1943
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 got an operator= that does deep copy, and a copy constructor that does deep copy... is there any way for me to get a shallow copy without writing a function that maually copies each member datum into an out parameter? I'd love to be able to just...
26
15816
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
2001
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 different.) It's hard for me to recall the question exactly. But basically, it involved a class construction with a shallow copy constructor. The question was basically "What is the bug in this code?" The intended answer was that shallow...
4
3150
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
8752
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 format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as suggested by O'Reilly (using the "ICloneable" inhereited interface), at least for the .NET framework. ...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10031
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9079
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4146
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 we have to send another system
2
3762
muto222
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.