Computer Science Canada A little bit a clarification with returning objects. |
Author: | Aziz [ Sun Jul 30, 2006 1:51 pm ] | ||||
Post subject: | A little bit a clarification with returning objects. | ||||
In the following code:
and later:
In my understanding, the 'getElement()' returns a pointer to the 'type' member, right? So, if something like changeType() is called on 'typeOfElement', does this change 'type' in the first code snippet? 'typeOfElement' and type point to the object, right? I want to make 'typeOfElement' a new object, but with all the same information as 'type'. Would I use the copy() method, or what? |
Author: | bugzpodder [ Sun Jul 30, 2006 1:56 pm ] |
Post subject: | |
there are no pointers in java. all objects are pass by reference. to make another copy, either implement the copy constructor or override the clone method |
Author: | Aziz [ Sun Jul 30, 2006 2:16 pm ] |
Post subject: | |
Okay, thought so, thank you for the clarification ![]() |
Author: | wtd [ Sun Jul 30, 2006 2:57 pm ] | ||
Post subject: | |||
Consider the following. I use Scala because it has an interactive interpreter and syntax I find nicer to work with than Java. The semantics remain relevant, though.
|
Author: | Aziz [ Sun Jul 30, 2006 3:49 pm ] | ||
Post subject: | |||
Yes, I see, and I actually understood that. Anyways, another question.
I want to be able to compare if the object is elemental, and if it is a ceratin type (ie FireElement) but I'm not sure if 'return type;' is safe, as anything else could change it. How would I implement a copy() or clone() method in Element, I've tried and no succes. The CloneNotSupportedException is throwing me off, even when I implement clone. |
Author: | wtd [ Sun Jul 30, 2006 5:26 pm ] |
Post subject: | |
Can I see your other classes? |
Author: | wtd [ Sun Jul 30, 2006 8:13 pm ] | ||
Post subject: | |||
bugzpodder wrote: there are no pointers in java. all objects are pass by reference.
Actually, it's worth noting that you have this exactly backwards. In Java all Object type variables are handled by pointers. You just can't arbitrarily manipulate these pointers. These pointers are then passed by value to methods. Were they pass by reference, we could do something like:
|
Author: | Aziz [ Sun Jul 30, 2006 10:07 pm ] | ||||||
Post subject: | |||||||
I've included the appropriate source files. Thanks wtd.
|
Author: | wtd [ Sun Jul 30, 2006 11:01 pm ] |
Post subject: | |
Well, one perhaps less than immediately relevant suggestion: The methods in your Elkemental interface should probably be getElementType and setElementType. I'll look more closely tomorrow. |
Author: | bugzpodder [ Sun Jul 30, 2006 11:13 pm ] |
Post subject: | |
yes you are right wtd. in fact i think i've posted a site a while ago that explains this clearly than most others. it made two points which kind of amusing. cant find it though. |