dcsimg
How to clone Objects
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   lemort_christophe
Posted On:   Friday, June 13, 2003 01:36 AM

Hi, In a given class (let us say class A), I would like to have a private field (let us say field B) defined as an Object so as to store either: Integer objects Double objects Strings objects according to the context. I am trying to define a copy constructor for the class A. In this copy constructor I have to copy the field B but I don't want to look at the real class of the field B. I tried to write something that looks like: A( A sourceObj ) { B = sourceObj.B.clone(); //B is declared as an Object } It doesn't work because the clone method of the Object class is defined as protected. Is there another way to do the same job (to avoid casting and to keep the   More>>

Hi,

In a given class (let us say class A), I would like to have a private field (let us say field B) defined as an Object so as to store either:
Integer objects
Double objects
Strings objects
according to the context.

I am trying to define a copy constructor for the class A. In this copy constructor I have to copy the field B but I don't want to look at the real class of the field B.

I tried to write something that looks like:
A( A sourceObj ) {
B = sourceObj.B.clone(); //B is declared as an Object
}

It doesn't work because the clone method of the Object class is defined as protected. Is there another way to do the same job (to avoid casting and to keep the B field as generic)

Thanks a lot

Christophe

   <<Less

Re: How to clone Objects

Posted By:   Christopher_Koenigsberg  
Posted On:   Friday, June 13, 2003 08:10 AM

What if B is something that should not be cloned in the first place? (a human being :-), a singleton, something with volatile/sensitive state, ...)

About | Sitemap | Contact