Search code examples
qore

How to test if Qore object is inherited from a specific class


When I have more classes, how can I test if instance is derived from a class?

class a {
};
class b inherits a {
};
class c inherits b {
};


a B = new b();
a C = new c();
assert ((B is instance_of b) == (C is instance_of b))

Ugly hack is testing B.className == 'b' but it is wrong for 'C'. I cannot find an operator.


Solution

  • use the instanceof operator:

    class A {
    }
    class B inherits A {
    }
    class C inherits B {
    }
    
    A a();
    B b();
    C c();
    
    printf("%y %y %y\n", a instanceof B, b instanceof B, c instanceof C);
    

    prints: False True True

    (the code above is based on your code but follows Qore's standard naming conventions and was also corrected for syntax errors)