Jump to content

Returning base class member from derived class in UnigineScript


photo

Recommended Posts

This one caused some really nasty bug in our code:

class A {};
class B {};
class C {
    A a;
    C() { a = new A(); }
};
class D : C {
    B b;
    D() { b = new B(); }
    A getA1() { return a; }
    A getA2() { assert( a is A ); return a; }
};

Here getA1() actually returns D::b, while getA2() does return C::a. To make things worse, if you run this with NDEBUG, getA2() changes it behaviour and also returns D::b. So it seems that if you don't explicitly touch a member of your base class, a random variable from the derived class is returned instead. This one in particular is nasty as that it doesn't cause a compile error and neither happens in debugging, since the assertion prevents the bug from happening.

Link to comment

Hello, you are absolutely right. I've added this bug to our internal bug tracker

For now you can use this workaround:

class A {};
class B {};
class C {
    A a;
    C() { a = new A(); }
};
class D : C {
    B b;
    D() { b = new B(); }
    A getA1() { return C(this).a; }
};
Link to comment
×
×
  • Create New...