🎉 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!

"No default constructor for object of type 'MyClass'" in MyClass::opAssign

Started by
5 comments, last by WitchLord 5 years, 7 months ago

Why does this script error? What's the correct way to write a boilerplate opAssign? (You can probably tell I'm not too experienced with C++.)


class MyClass {
    int x;

    MyClass(int x) {
        this.x = x;
    }

    MyClass opAssign(const MyClass &c) {
        x = c.x;
        return this;
    }
}

I tried adding an empty default constructor (MyClass() {}) and that made it compile but enter an infinite loop when opAssign is called. I don't want to have a default constructor (that means constructor with no arguments?), it doesn't make sense to have a default value for x in my domain.

Advertisement

http://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_class_ops.html#doc_script_class_assign_ops

Your opAssign is declared to return a copy of the MyClass instance (return by value), that is why the compiler tries to make a copy by first creating a default instance and then assign the values to it. 

To be correct your opAssign method should be returning the instance by reference or by handle. 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Ah yes thank you.

I'm not sure if this is a bug, but when I call a method of a null object, execution of the script stops completely with no error:


class MyClass {
    void foo() {}
}

void id() {
    print('marco ');
    MyClass a = null;
    a.foo();
    print(' polo'); // never reached, no error
}

I'm also executing the script from inside a dll-proxied monstrosity, so I'm not sure how this snippet would behave in a more normal setting.

This would cause a null pointer exception, which correctly interrupts the script execution. Your application code (in the dll) is probably not showing the exception information (which would point out the type of the exception and the location where the exception occurred).

 

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

 Does Angelscript catch null pointer exceptions? I didn't know that. That would imply that the crashes I've been getting are access violations from the dll, not from my script.

Yes, null pointer accesses within the scripts are caught by the VM, so you shouldn't be seeing any crashes due to that. Of course, I cannot rule out potential bugs, so if you have some case you think should have been caught by the VM but wasn't, please do let me know.

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement