Daniel said:
From what I've read I'd argue that copy constructors are not better,
but a more risky construct.
What that you have read about them that leads you to believe this ? Please
give technical arguments.
Considering the Composition arguments
combined with not being able to copy an object safely as its concrete
type(and downcast to the variable type) without knowing the type
ahead of time
Come again ? A copy constructor always defines the type which is to be
copied at compile time as part of the signature of the constructor. I have
no idea to what the term "Composition arguments" refers.
, or that without documentation you cannot know for sure
that you are calling a copy constructor, not just a badly typed
constructor, or an ambigious constructor call using some of the more
complicated language rules that not everyone is familiar with
Without documentation programmers are going to suffer no matter what your
methods are called. Why would anyone want to create a badly typed
constructor, and how would this differ from a badly implemented Clone()
method ? As far as "complicated language rules that not everyone is familiar
with", programming is not a popularity contest nor are copy constructors any
more complicated than any other language construct.
, I'd
call Copy Constructors a technique that lacks a good deal of grace.
If your opposition to copy constructors are that they are not aesthetically
pleasing to you, it is probably because you haven't used them very much. I
assume that is what you mean by "grace" in this context.
Calling Clone tells me I have a copy of an object, new
myObject(myObjectReference) leaves me wondering if I'll get a copy or
a aggregate object, or simply an object that takes a limited set of
settings from another object(perhaps preferences without data).
"Using a copy constructor tells me I have a copy of an object, while calling
Clone() leaves me wondering if I'll get a copy or a aggregate object, or
simply an object that takes a limited set of settings from another
object(perhaps preferences without data)."
It
also guarentees that I won't lose any data that makes sense to copy.
A copy constructor very well could ignore many fields which shouldn't
be dumped, simply because you called a base constructor instead of
the concrete type constructor. I don't consider a copy without
copying ALL relevent data a copy, but more of a near type conversion.
What is relevant data is determined by the designer of the classes in the
hierarchy in which the copy constructor resides. Why is Clone() any better ?
What guarantee do I have that all relevant data has been copied from the
current object and its base classes into the object I have cloned ? It is
once again a decision of the designer of the class.
It also doesn't help as far as interfaces go, how exactly do you
clone an object that implements, say, IMyDataObject with a copy
constructor?
Interfaces are member function contracts and do not contain data AFAIK.
What constructor do you call?
See above.
Is there an argument for why copy constructors are better, other than
simply because they are easier(for you) to type? The ease of
constructors calling eachother doesn't hold up
It certainly is easier to call the base class's constructor to initialize
base class data from a copy, and initialize just the data in your own class
from a copy, than having to initialize all data in all base classes
including your own class from a copy. I believe you have to be kidding to
feel otherwise. As I also previously pointed out, there is no way to
initialize private base class data in my derived class when doing Clone()
since I have no access to it. But that is not a problem using copy
constructors since each class in hierarchy initializes its own data from
the copy used to construct the original object.
, you can use protected
constructors or a protected virtual method to do the same thing:
protected virtual Clone(BaseType sourceObj, BaseType newObj); in a
polymorphic fashion(although it'd require some casting and type
checking), depending on your needs.
I have no issue with protected copy constructors. Another poster gave me a
very good method of implementing Clone() using protected copy constructors.
That just shows me that the copy constructor technique is not flawed. That
technique is certainly a workable implementation for Clone() and the only
way to copy private data from the base class.
And I personally find that
calling new to create a copy is not anywhere near as clear, from a
maintainablitiy and readability standpoint as .Clone() is.
Again that is an aesthetic call. If you have programmed in a language where
copy constructors create an easy to program methodology of creating an
object based on another object of the same type, than copy constructors are
more natural than Clone().
The only
argument I've seen for copy constructors is that its easier for C++
developers to use, and an unsubstantiated "its better".
Please, explain why its better, not simply by basing it on C++, but on
technical, .NET reasons its better. This is not an entirely C++
world, and some pieces of C++ are probably better lost.
I have explained why it is better a number of times in this thread. However
the technique of creating Clone() using a protected copy constructor is fine
with me, and does have the added attraction that once I implement it, other
objects know I am cloneable. I don't believe your own arguments have
technical merit but rely on aesthetics and "unknown" sources. Please argue
the technical issues youirself and not bring up language prejudices. I am
not supporting copy constructors because it is C++ but because it is an easy
and safe method to create an object based on another object. If it can be
abused, it is no different from an implementer of Clone() abusing that
functionality. Finally, as I have repeatedly shown, unless Clone() is
implemented using copy constructors, it is broken if base classes have
private data that need to be cloned also.