Re: why no copy constructor in java?



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.

.



Relevant Pages

  • Re: I am confused with these concepts.
    ... out of scope this function, ... But because we return myClass, ... It's up to you to decide whether to use a pointer or not. ... copying a class instance might be very expensive. ...
    (microsoft.public.vc.language)
  • Re: TRICK: Unique IDing
    ... > pointer to the target instance or a pointer to a copy of the target ... When passing an instance of a class it can be done so the ... > class MyClass ... > you want ) and compare it against the suspect. ...
    (microsoft.public.dotnet.languages.vc)
  • TRICK: Unique IDing
    ... Sometimes it's hard to get straight when passing or storing or returning an ... pointer to a copy of the target instance. ... class MyClass ... ID) and compare it against the suspect. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Conversion from void* to a point of a class
    ... Well, that is true, dynamic_cast does not work for void-pointers - once you have converted your MyClass* pointer to a void* you have voluntarily given up all type information and there is no safe way back. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointers in VC.Net
    ... that you may not actually need to create a pointer for your specific needs ... The .Net platform supports passing objects around as references ... MyClass class = new MyClass; ... What language are you using? ...
    (microsoft.public.dotnet.framework.clr)