Computer Science Canada

Figuring out what class a variable is

Author:  smool [ Sat Dec 10, 2011 1:48 am ]
Post subject:  Figuring out what class a variable is

What is it you are trying to achieve?
Say I have a Weapon parent class, and then a Ranged child class and a Melee child class, is there a way to check to see if my pointer is pointing to a Ranged class or Weapon class?

maybe something like:

Turing:

import Weapon, Ranged, Melee

var w : ^Weapon

if w Is Ranged then
    put "You are carrying a ranged weapon"
elsif w Is Melee then
    put "You are carrying a melee weapon"
end if


is there any way to do that aside from making boolean variables in the parent class and calling those?

Author:  Nick [ Sat Dec 10, 2011 6:27 am ]
Post subject:  RE:Figuring out what class a variable is

I think you're looking for objectclass. Take a look at the following

Turing:

class Foo
end Foo

class Bar
    inherit Foo
end Bar

class Baz
    inherit Foo
end Baz


var foo : ^Foo
new Foo, foo
var bar : ^Bar
new Bar, bar
var baz : ^Baz
new Baz, baz

if objectclass (foo) = Foo then
    put "foo is Foo"
end if

if objectclass (bar) >= Foo then
    put "bar is at least Foo"
end if

if objectclass (foo) < Baz then
    put "foo is a parent of Baz"
end if

Author:  smool [ Thu Dec 15, 2011 4:59 pm ]
Post subject:  RE:Figuring out what class a variable is

thx Very Happy


: