Deep Copy and '='

G

Guest

Hello, Newsgroupians!

I have, perhaps, a simple question regarding deep copy and the assignment
operator. I'm creating a class, and I would like to perform a deep copy of
all the members in an instance of the class. I know I can derive the class
from ICloneable and override the Clone() function. I do this, and I can
accurately create a deep copy of my class.

MyClass c1 = new MyClass(...);
MyClass c2;
c2 = c1.Clone();

This works -- of course -- but I would like to reduce the amount of typing
to just the simple...

c2 = c1;

Many of you may state that this is undesirable, for you may want a reference
at one time or another, but in this specific class whenever I do an
assignment operator, I ALWAYS want a deep copy. Is this possible? Thank you
all.


Trecius
 
J

Jon Skeet [C# MVP]

Many of you may state that this is undesirable, for you may want a reference
at one time or another, but in this specific class whenever I do an
assignment operator, I ALWAYS want a deep copy. Is this possible? Thank you
all.

No, it's not. You can't override the assignment operator.

Jon
 
R

Rene

I don't think you can do that with classes but you can always try changing
the class to a strcut.



If your requirements allow you to do that, you will be able to accomplish
what you want.
 

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