Re: Copy constructor

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Nick,

See inline:

ICloneable<T> wouldn't give you anything.
If you know an object is ICloneable and a T then it follows that it must
be safe to cast Clone() to T.

Not true. If I had ICloneable<T> then the Clone method would return a
string, not an object, and the compiler can verify that my assignments are
valid. For example, in .NET 1.1 and before, you can do this:

string s = "This is a string.";
int i = (int) s.Clone();

And the compiler would not detect it. The error would occur at run
time.

If IClonable was generic, then you would do this (hypothetically):

string s = "This is a string.";
int i = s.Clone();

The compiler would not allow this, because the implicit implementation
of ICloneable<string> would return a string, and the compiler would
recognize that is not a valid assignment.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx



"Nick Hounsome" <nh002@xxxxxxxxxxxxxxxxxx> wrote in message
news:SCLEf.256812$D47.214926@xxxxxxxxxxxxxxxxxxxxxxxxxxxx

[I just realized that it would save you the boxing]

NB I always implement ICloneable.Clone explicitly and provide an
additional strongly typed Clone method for the case where you would use a
copy constructor in C++. IMHO this is a nobrainer.

"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:umUwWeNKGHA.500@xxxxxxxxxxxxxxxxxxxxxxx
Dave,

In this case, the only advantage you have with a "copy" constructor is
that you have type checking at compile type, whereas with IClonable, you
have to use a cast, which blows up in your face if the cast fails.

I prefer having the type checking, personally, and would use that over
IClonable.

What I ^really^ would have liked would have been to see IClonable<T>,
but alas, it is not in the framework.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:432E8260-8CB5-48B3-9328-DABB201A6A98@xxxxxxxxxxxxxxxx
OK I see what you mean. Nevertheless my question still stands - given
that
you have to either *explicitly* call the copy constructor, or
*explicitly*
call the Clone method, is there any advantage of one over the other?
The temptation to override the assignment operator in all my classes to
get
back to a C++ way of life is almost overwhelming.
--
Dave


"Nicholas Paldino [.NET/C# MVP]" wrote:

Dave,

In comparison to C++, no, there is no such thing. Yes, you can
create a
constructor that will take a reference to the same type, but that will
not
give you the assignment semantics that copy constructors give you in
C++.
You still have to make the call explicitly to the constructor, which is
the
point I am trying to make. To C#, it's all the same, and it is not
treated
differently, unlike the C++ compiler treats it.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B86D1346-6B38-47CA-A45C-286B6FDF15BA@xxxxxxxxxxxxxxxx
This puzzles me. First of all, saying "The IClonable interface is the
closest
thing to a copy constructor" implies that there is no copy
constructor in
C#,
which of course there is.
So what is the difference between ICloneable and a copy constructor.
There
appears to be none, in which case what is the purpose of ICloneable?
Ollie
Riches says "usually in my experience the Clone method usually calls
the
copy
constructor internally" - so why bother with ICloneable at all if a
public
copy constructor would be just as good?
--
Dave


"Nicholas Paldino [.NET/C# MVP]" wrote:

Arne,

The IClonable interface is the closest thing to a copy
constructor.
However, you have to manually invoke the Clone method on the
IClonable
interface, it doesn't get called for you on assignment.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Arne" <Arne@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:46D09AC5-5D1C-4219-B36F-E63114FC2CEC@xxxxxxxxxxxxxxxx
In C++ we have a copy constructor. What is the equivalent in .Net?
Would
that
be a clone method?












.



Relevant Pages

  • Re: operator=
    ... Compiler will supply - unless the programmer decides to do it - the ... It isn't required in an assignment: ... You will find that the code within the copy constructor and assignment ... Aside from the inlined code [i.e. the member ...
    (alt.comp.lang.learn.c-cpp)
  • Re: basic basic ada question
    ... Assignment statements, you mean. ... a name of the primitive operation. ... constructor or alternatively object state changer. ... Surely the calls must be inserted by the compiler. ...
    (comp.lang.ada)
  • Re: Copy constructor
    ... the only advantage you have with a "copy" constructor is ... have to use a cast, which blows up in your face if the cast fails. ... call the Clone method, is there any advantage of one over the other? ... The temptation to override the assignment operator in all my classes to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Memory leak - What the ...?
    ... The compiler-generated copy constructor and assignment ... operator are defined to do memberwise copying, ... The compiler may implement these more efficiently ...
    (comp.lang.ada)
  • Re: Copy constructor
    ... The temptation to override the assignment operator in all my classes to get ... You still have to make the call explicitly to the constructor, ... Riches says "usually in my experience the Clone method usually calls the ... The IClonable interface is the closest thing to a copy constructor. ...
    (microsoft.public.dotnet.languages.csharp)