G
Guest
In C++ we have a copy constructor. What is the equivalent in .Net? Would that
be a clone method?
be a clone method?
Nicholas Paldino said: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]
- (e-mail address removed)
Arne said:In C++ we have a copy constructor. What is the equivalent in .Net? Would
that
be a clone method?
Dave said: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 said: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]
- (e-mail address removed)
Arne said:In C++ we have a copy constructor. What is the equivalent in .Net?
Would
that
be a clone method?
Nicholas Paldino said: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]
- (e-mail address removed)
Dave said: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 said: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]
- (e-mail address removed)
In C++ we have a copy constructor. What is the equivalent in .Net?
Would
that
be a clone method?
Nicholas Paldino said: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]
- (e-mail address removed)
Dave said: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 said: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]
- (e-mail address removed)
In C++ we have a copy constructor. What is the equivalent in .Net?
Would
that
be a clone method?
Dave said: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 said: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]
- (e-mail address removed)
Dave said: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
:
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]
- (e-mail address removed)
In C++ we have a copy constructor. What is the equivalent in .Net?
Would
that
be a clone method?
Nicholas Paldino said: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]
- (e-mail address removed)
Dave said: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 said: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]
- (e-mail address removed)
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
:
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]
- (e-mail address removed)
In C++ we have a copy constructor. What is the equivalent in .Net?
Would
that
be a clone method?
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.
Nick Hounsome said:[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 said: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]
- (e-mail address removed)
Dave said: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
:
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]
- (e-mail address removed)
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
:
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]
- (e-mail address removed)
In C++ we have a copy constructor. What is the equivalent in .Net?
Would
that
be a clone method?
Dave said: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.
Bruce Wood said:Now, I'm no generics guru, but then that's why I thought to ask this
question: so I can learn.
Correct me if I'm wrong, but if the Framework offered an ICloneable<T>
interface, then how would you write code that required nothing more
than that an object be cloneable? For example, currently I can write
this:
public object MyFunction(ICloneable abc) { ... }
and what that means is that abc must have (at least) a Clone() method.
However, if we introduce ICloneable<T>, and I declare this:
public class MyClass : ICloneable<MyClass>
{
...
public MyClass Clone() { ... }
}
and then try to do this:
MyClass abc = new MyClass();
MyFunction(abc);
won't the compiler give me an error, saying that "abc" does not
implement ICloneable... because it doesn't: it implements
ICloneable<MyClass>, which is not the same thing. If I then try to say:
public MyClass : ICloneable<MyClass>, ICloneable
{
}
then I end up with problems of ambiguous reference, because when I say:
MyClass def = abc.Clone();
which method am I calling... the one that returns a MyClass
(ICloneable<MyClass>) or the one that returns an object (ICloneable)?