some issues with external abstract method

About Monkey 2 Forums Monkey 2 Development some issues with external abstract method

This topic contains 4 replies, has 2 voices, and was last updated by  abakobo 2 years, 6 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #3612

    abakobo
    Participant

    I met two troubles with external abstract classes:

    1-private is not accessible to overriden methods declared in mx2 (may be the same when just overriden)
    2-you can’t pass by reference to pure virtual methods that had been implemented in mx2 tough by value and by pointer is ok (pointers are value?). And you can pass by reference to non overriden external classes. (may be the same when just overriden)

    Issues posted on github with example files (attached to this post too) as I suppose these are bugs or at least limitations.

    Point 2 prevents me from cleanly implementing the box2d b2Draw class in mx2 (wich is essential for debugging). Hope I won’t have to wrap it..

    #3618

    Mark Sibly
    Keymaster

    private is not accessible to overriden methods declared in mx2 (may be the same when just overriden)

    Use ‘Protected’ instead for when you want only subclasses to be able to access members.

    you can’t pass by reference to pure virtual methods that had been implemented in mx2

    This is because mx2 has no concept of ‘const &’ so there is no way it can generate c++ code with ‘const &’ params.

    So your MyVM(i:Int) override does not in fact override the native superclass method (which takes a const int& param), so the superclass remains abstract.

    The best solution in this case is probably to create a glue class for problematic methods like this, eg:

    It’s kind of ugly, but you only have to glue up the non-mx2 compatible methods. I did something like this for litehtml and it wasn’t too bad.

    Or, if you can find a ‘C’ wrapper, that’s probably even better!

    I’m not sure why the author went with ‘const int&’ here in the first place (or perhaps this is just a test case?) – plain ‘int’ would do the same thing and be faster (and be compatible with mx2!) – but that’s c++ for you. There are just so many ways to do things it’s all too easy to pick a crappy way without even knowing it.

    Unfortunately, this wont work in every case, eg: with factory systems where you don’t get a chance to subclass. But it should work for this.

     

    I ultimately intend to use libclang or swig to write a c/c++ extern generator (I actually think this should be quite high priority, but I’ll get the mobile stuff out of the way first) which will help with writing lib modules and perhaps SOME of the glue stuff, but there will likely always be problematic c++ types.

    #4242

    abakobo
    Participant

    can we now pass by reference to externals? I have some const& that are passing fine now though I tought I would have to remove them from c++ code!

    edit: it was not an abstract method so that must be the point.

    #4250

    Mark Sibly
    Keymaster

    C++ allows you to pass a ‘value’ to a ‘value&’ param, although you need to be a bit careful as unless it’s a ‘const value&’ the c++ code can modify the value you pass it.

    So if the c++ interface looks like this:

    …then it’s possible that after calling this…

    …that i no longer contains 10!

    However, with this:

    You can be sure blah does not change the value of ‘r’, thanks to the ‘const’ prefix.

    (Also, you can replace ‘int’ in all these examples with any custom struct name and the same applies).

    You can think of  ‘&’ (reference) params as being very similar to pointers, only the compiler does the ‘varptr’ bit for you automatically (which implies arguments must be variables or you can’t varptr them), eg: this…

     

    …will actually produce very similar/identical assembly code to the first example above, so you can see how it’s possible for blah to modify ‘i’ as it really has a pointer to ‘i’.

    Probably just confused things there…

    #4264

    abakobo
    Participant

    I just forgot that passing by reference was an issue with abstract method only.
    However it should faster by value..

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

You must be logged in to reply to this topic.