🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Bug in type-casting

Started by
1 comment, last by dkrusu 9 years, 6 months ago

I've encountered a bug when using implicit ref casting. All of my widget objects inherit from a Base class that then has a implicit cast for each widget type. The findWidget() method returns a reference to a Base object and then is casted to the Label object. If I try to assign the reference to a class property it can't resolve the method call for setText(). Creating a local variable works just fine.


class CobaltTheme
{
    private const Widgets::Label @mClock;

    CobaltTheme()
    {
        @mClock = Window.findWidget("Home.Clock");    

        onTimerCallback @timerCallback = onTimerCallback(this._clockTimer);
        createTimer(1, timerCallback);
    }

    bool _clockTimer()
    {
        //  ERR  : No matching signatures to 'Label::setText(string) const'
        mClock.setText(timeToString("%I:%M%P"));

        // Works fine:
        Widgets::Label @Clock = Window.findWidget("Home.Clock");
        Clock.setText(timeToString("%I:%M%P"));
        return true;
    }
}

// The Base to Label reference cast
r = engine->RegisterObjectBehaviour("Base", asBEHAVE_IMPLICIT_REF_CAST, "Label@ f()", asFUNCTION(Base::castToLabel), asCALL_CDECL_OBJLAST); assert( r >= 0 );
    
r = engine->RegisterObjectMethod("Window", "Widgets::Base@ findWidget(const string &in)", asMETHOD(Window, findWidget), asCALL_THISCALL); assert( r >= 0 );

Thanks

Advertisement

From your code it looks like your problem is much more prosaic in nature: you're probably trying to call a non-const method on a const handle. That is what the error indicates (no matching signatures to 'Label::setText(string) const') and it's why the other part of your code works ("const Widgets::Label @mClock" vs "Widgets::Label @Clock").

Now I feel like an idiot! Not sure why I chose to make the class member a constant, and then some how overlooked that for a good 30-45 minutes of trying to figure out the problem.

Thanks Ementaler

This topic is closed to new replies.

Advertisement