Extending classes and Override question

About Monkey 2 Forums Monkey 2 Programming Help Extending classes and Override question

This topic contains 11 replies, has 4 voices, and was last updated by  wiebow 2 years, 10 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1083

    wiebow
    Participant

    Hello,

    I have a question regarding extending classes and overriding methods. In BlitzMax, in a base Type, I was able create and for instance, call that empty method in the New() method. In the extended type, I would override that method. Creating a new extended would result in the method being called automatically because the the base type New() method would would call it.

    Now in M2, I try the following:

    I expected the extended version of Startup being called, but instead the base version is called. This is different than how it was in Max.. Is this intended behaviour? And if so, does anyone have an idea to emulate ‘hooks’ ?

    I really liked this feature as it was really simple to call code automatically.

    Thanks!

    #1084

    therevills
    Participant

    This looks like a bug to me. In most languages the extended version of startup should be called…

    Java:

    [/crayon]

    Outputs:

    [/crayon]

    MX1:

    [/crayon]

    MX2:

    [/crayon]
    #1085

    Mark Sibly
    Keymaster

    > In most languages the extended version of startup should be called…

    Well, most languages except c++  (and I think Java gives an error).

    A bit more on this here:

    http://www.monkey-x.com/Community/posts.php?topic=10557

    I actually haven’t decided 100% which way I’m going here either.

    #1086

    therevills
    Participant

    [quote]Well, most languages except c++ (and I think Java gives an error).[/quote]

    Well I always thought C++ was a bit odd 😉

    Java outputs:

    [/crayon]

    Run the code in the above post, pure Java and no errors…

    #1087

    therevills
    Participant

    Playing with C#, using override and virtual:

    [/crayon]

    Outputs:

    [/crayon]

    Not using override and virtual keywords:

    [/crayon]

    Outputs:

    [/crayon]
    #1088

    Mark Sibly
    Keymaster

    Ok, looks like c++ enforces it, but other languages only warn about it:

    http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors

    https://msdn.microsoft.com/en-us/library/ms182331.aspx

    I think it’s generally a good rule – if a ctor calls a method that is overridden by a subclass, that method can no longer depend on it’s class fields being properly initialized because it’s ctor hasn’t been executed yet. Or just as bad, the override could be called, but then anything it did gets undone by the ctor being called later, eg:

    http://stackoverflow.com/questions/12100449/java-calling-superclass-constructor-which-calls-overridden-method-which-sets-a

    But I’ll likely end up sticking with what’s most practical/flexible on the translator side of things.

    #1089

    therevills
    Participant

    Just fired up Code::Blocks (last time I used that when I was testing the BMX 3D stuff!):

    [/crayon]

    Outputs:

    [/crayon]

    So as long as what MX2 is going to do is well defined and documented I dont think we can complain too much 🙂

    #1090

    wiebow
    Participant

    Nope, thanks for investigating this! It’s good to know and understand that some of the ways of working in Max are not valid anymore.

    #1092

    Jesse
    Participant

    so how do you access the method in the extended class are we still using the extended Class?

    #1093

    wiebow
    Participant

    You can access it, but only from the extended class, not from the base class.

    #1097

    Mark Sibly
    Keymaster

    Just to clarify, we’re talking about what happens when a constructor (and only a constructor) calls an overridden (ie: virtual) method.

    The mx2 behaviour might yet change before V1.0, but in general it’s not a good idea for a constrcutor to call an overridden method. I think this sums it up best:

    https://msdn.microsoft.com/en-us/library/ms182331.aspx

    #1098

    wiebow
    Participant

    Looking back at it now, I’m all for it. I see the possible negative consequences of the old way of doing it.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.