www.digitalmars.com         C & C++   DMDScript  

D - class hierarchy

reply "Dario" <supdar yahoo.com> writes:
Is there a way to check if a class is derived from another?
I mean something like "a.classinfo.isDerived(b.classinfo)".
Maybe object.d should include a function to do that.
I think it can be written as follows:
    class ClassInfo
    {
        byte[] init;
        char[] name;
        void*[] vtbl;
        Interface[] interfaces;
        ClassInfo base;
        void* destructor;
        void function(Object) _invariant;
        uint flags;
        void* deallocator;

        bit isDerived(ClassInfo c)
        {
            for(ClassInfo b=base; b!==Object.classinfo; b=b.base)
                if(b === c)
                    return true;
            return false;
        }
    }

I also have to advise the great confusion in the D math library.
"math.d" contains functions like "double pow(double,double)" and "real
pow(real,int)"
which inevitably conflicts with each other. Moreover I saw that the sin
function in "math.d"
is still the old C function, not the optimized D intrinsic one. These
conflicts together too.
I know that explicit casts can resolve any conflict, but I think that
explicit casts are
annoying and cumbersome. Why should "pow(3.0, 0.5);" be ambiguous?
Dario
Apr 28 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Dario" <supdar yahoo.com> wrote in message
news:b8kabs$75t$1 digitaldaemon.com...
 Is there a way to check if a class is derived from another?
Yes. Try a down cast, if it produces a non-null value it is derived from the other. For example: class A { } class B : A { } int isB(A *a) { return cast(B)A !== null; }
 I also have to advise the great confusion in the D math library.
 "math.d" contains functions like "double pow(double,double)" and "real
 pow(real,int)"
 which inevitably conflicts with each other. Moreover I saw that the sin
 function in "math.d"
 is still the old C function, not the optimized D intrinsic one. These
 conflicts together too.
 I know that explicit casts can resolve any conflict, but I think that
 explicit casts are
 annoying and cumbersome. Why should "pow(3.0, 0.5);" be ambiguous?
I agree that all needs work.
Jul 13 2003
parent Dario <Dario_member pathlink.com> writes:
Dario:
 Is there a way to check if a class is derived from another?
Walter:
 Yes. Try a down cast, if it produces a non-null value it is derived from
 the other. For example:

 class A { }
 class B : A { }

 int isB(A *a)
 {
    return cast(B)A !== null;
 }
Wow! =)
Jul 14 2003