Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-03-2003, 08:23 AM   #1 (permalink)
Mjolnir
Registered User
 
Mjolnir's Avatar
 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 5
Mjolnir is on a distinguished road
Cloning

Is this valid code for a shallow clone?

x=(MyClass) y.clone();
Mjolnir is offline   Reply With Quote
Old 05-04-2003, 02:17 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
no
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 05-04-2003, 05:52 PM   #3 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
maybe.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 08-03-2003, 11:20 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
It's valid only if MyClass implements clone(), as it's not implemented by Object, only defined.

Personally, I think calling a method on the same line as class casting is a bit ambiguous. You probably ought to make y an instance of MyClass as soon as y is defined. For instance, if you get it out of an ArrayList:

MyClass y = (MyClass)list.get(i);
MyClass x = y.clone();

I don't know if that's possible in your situation, but you might want to give it a shot.

I've never heard of the term "shallow clone" before. What is it?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-03-2003, 02:50 PM   #5 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
Quote:
Originally posted by Belisarius
It's valid only if MyClass implements clone(), as it's not implemented by Object, only defined.
What makes you think that?

Read this: http://forum.java.sun.com/thread.jsp...rt=0&trange=15

And then: http://developer.java.sun.com/develo...a/Chapter3.pdf
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 08-03-2003, 04:04 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
The Java API. See clone() in Object:

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-03-2003, 04:08 PM   #7 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Anyways, Josh Block in Effective Java spends a lot of time says that unless you have a very specific need for clone, don't use it. Chances are if you're asking about how to use clone in such a simple context, you shouldn't be using it.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-03-2003, 05:31 PM   #8 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
Quote:
Originally posted by Belisarius
implements clone(), as it's not implemented by Object,
That was a little confusing, I thought you meant it
did not implement the "clone()" method, which it does.

That was my point.
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 08-03-2003, 05:41 PM   #9 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
I might be mangling my jargon. I meant that it doesn't implement clone() as one implements methods in a class that implements and interface, but it does define the method as methods are defined within that interface.

Am I making sense?
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:50 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting