473,788 Members | 2,848 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 1825

"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
4534
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
3288
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
12352
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
10120
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
1942
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
15815
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
3149
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
9656
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
9498
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
10364
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
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
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...
0
5398
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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
3670
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.