Computer Science Canada Casting a pointer in Turing |
Author: | DemonWasp [ Tue Feb 17, 2009 4:33 pm ] | ||||
Post subject: | Casting a pointer in Turing | ||||
I searched this forum, but couldn't find anything that makes mention of this. If I'm duplicating something somewhere else, it's by accident. You may have noticed that Turing doesn't have a way of casting a base-class-pointer to a derived-class-pointer like most other languages. In Java you would just do
No such luck in Turing. But, there IS a way. It's grisly though, so those with an aversion to horrific implementations may want to avert their eyes.
Weirdly enough, you can't just use ^Derived in place of DerivedPtr there. Turing expects an identifier, which apparently doesn't include pointers. By making an object-ptr type, however, you can easily cast to that type with just one line. edit: Quotefail |
Author: | saltpro15 [ Tue Feb 17, 2009 4:44 pm ] |
Post subject: | RE:Casting a pointer in Turing |
I did not understand more than 3 words of that program, what the heck does a "cheat" do? better yet, what does any of it do ? |
Author: | DemonWasp [ Tue Feb 17, 2009 5:03 pm ] | ||
Post subject: | RE:Casting a pointer in Turing | ||
If you haven't read Cervantes' tutorial on Turing Classes, start there. Otherwise... Basically: You have two classes, call them "Base" and "Derived". Derived inherits from Base, so we can say "Derived is a Base" but we CANNOT say "Base is a Derived" because that's not always true. Turing understands that, so any variable you have that is defined as a pointer-to-Derived can access all the methods exported by Base, because it IS a Base. However, there are numerous circumstances where you have to be more specific. What if you need one of the methods that's only implemented by Derived, but for some reason you have to access it within a variable declared as ^Base (pointer-to-Base)? Well, then you have to do what's called "casting". This just means "assume that this variable is a pointer-to-Derived, even though we don't know that for sure". Once we've done that (assuming it worked out...), we can access all the more specific fields and elements. Update: I took a look at what it does if you use cheat() to cast to something that it isn't. It looks like Turing will let you do it, no matter how dangerous it is; the result is something like unions in C. Try the following:
|
Author: | saltpro15 [ Tue Feb 17, 2009 6:27 pm ] |
Post subject: | RE:Casting a pointer in Turing |
ah, thanks |
Author: | DemonWasp [ Mon Apr 20, 2009 4:49 pm ] | ||
Post subject: | RE:Casting a pointer in Turing | ||
Resurrects to point something out: Turing's documentation is full of fail. Turns out there's an anyclass root class (like Java's Object class). In the documentation for that class, it notes the following: You can make a cast (with type-checking) from base-ptr to derived-ptr as follows:
It's a little cleaner. |
Author: | kalev [ Thu Apr 23, 2009 9:00 am ] | ||||
Post subject: | Re: Casting a pointer in Turing | ||||
theres an easier way to cheat:
opposed to:
note: the first way is shorter, and doesn't need to know the type of variable1 or variable2 |