Re: why no copy constructor in java?
- From: "AndyRB" <xandyrb@xxxxxxxxxxx>
- Date: 22 Dec 2005 16:15:42 -0800
Xiaoshen Li wrote:
> Dear All,
>
> Sorry for coming back to the old topic. I really cannot understand what
> some people has said before. Here I am not interested in clone(), sorry.
>
> In C++, without copy constructor,
>
> MyClass Obj_1 = new MyClass(args);
> MyClass Obj_2 = Obj_1;
The above is incorrect it should either be:
MyClass * Obj_1 = new MyClass(args);
MyClass * Obj_2 = Obj_1;
Here only the pointer value is copied, both pointers will then point to
the same object.
or
MyClass Obj_1 = MyClass(args);
MyClass Obj_2 = Obj_1;
Here either the default memberwise shallow copy will be performed or
maybe a deep copy if the default copy constructor has been overridden.
>
> will make Obj_1 and Obj_2 share the same private object if MyClass has a
> private variable which is class type.
If MyClass has a pointer to allocated data and the class performs the
default memberwise shallow copy then Obj_1 and Obj_2 will both have
pointers pointing to the same data. This is the basic issue regarding
shallow copying of objects (where this sharing of a resource is not
desired or not intended).
> C++ textbooks say that this
> behaviour(shallow copying) is usually un-acceptable. So we need to
> provide our own copy constructor, to make sure Obj_1 and Obj_2 have the
> identical, but separate private object(deep copying).
>
> In JAVA,
> MyClass Obj_1 = new MyClass(args);
> MyClass Obj_2 = Obj_1;
>
> will make Obj_1 and Obj_2 two names( two references ) for the same
> object. JAVA is happy and accepts such behaviour. It seems to me it is
> shallow copying in C++.
In the above only the reference is copied and not the underlying
object, however, this doesn't necessarily create any issues as the
garbage collector is able to track when all references to the object
no-longer exist and know when the object's memory can be reclaimed. The
problem occurs when a copy results in more than one object sharing a
resource and both objects acting as though they are the sole owner of
that resource, i.e. they have not been designed to share the resource
or maybe the sharing was unintentional...
>
> My questions is: why C++ cannot accept the shallow copying and JAVA can
> and is happy to live with it?
The issue mainly crops up with value semantics (and particularly where
objects are passed-by-value) because the default copy-semantics only
performs a shallow copy, for example...
MyClass Foo()
{
MyClass Obj_1 = MyClass(args);
return Obj_1;
}
Imagine MyClass has a dynamic buffer and performs a shallow copy to the
object that will be returned from function foo. Obj_1 will then be
destroyed and, assuming the destructor does what it should and
deallocates the buffer, the pointer in the returned object will have
become invalidated since it was pointing to the dynamic buffer which
has now been deallocated.
If you change foo to use reference rather than value semantics, and
therefore behave more like you would expect in Java, the issue no
longer exists as no copying of the object will take place :-
std::auto_ptr<MyClass> Foo()
{
std::auto_ptr<MyClass> Obj_1 = new MyClass(args);
return Obj_1;
}
>
> Thank you very much.
.
- Prev by Date: Re: Eclipse 3.2 plugin tutorial
- Next by Date: Re: Object streams and Socket network trouble
- Previous by thread: Re: why no copy constructor in java?
- Next by thread: using XPATH on XSD schema to get valid XML tags
- Index(es):
Relevant Pages
|