Jump to content
  • Advertisement
Dinkleberg

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

Recommended Posts

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.

Edited by Dinkleberg

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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).

 

 

Share this post


Link to post
Share on other sites

 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.

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!