const pointers

This topic contains 5 replies, has 2 voices, and was last updated by  codifies 2 years, 5 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #4975

    codifies
    Participant

    in a module I have something like this

    which I use like this

    I gotta be doing this wrong ! it seems a bit fugly!!!

    If I try to use a const pointer like a normal pointer I see g++ errors (off the top of my head about casting pointers)

    #4976

    abakobo
    Participant

    Defining extern with ‘const something’ is very limited. It can lead to some mess if you don’t use for functions arguments only. Because the transpiler will just copy the defined name and ‘const something’ is more than just the name on The C side. Mark has manualy made some glue code for these situation. For example for the chipmunk debugdraw drawpoly callback function that have a ‘const ptr’ as argument.
    I had also a similar problem with pure virtual methods for the box2d wrap but I chose to remove the const in the cpp code because box2d is solid enough not to need the const barrier imo. So if you may remove ‘const’ from the C code, it’s probably the fastest way.
    It’s one of the difficulties with c to mx2. The only one I encoutered for now.

    #4982

    codifies
    Participant

    I *really* don’t want to modify the C++ code its a large code base and will require periodic updating

    I am aware of what a const pointer is on the C side of things having used them in previous C projects, just a shame that MX2 doesn’t have some mechanism to handle them (or rather more gracefully) I might have misunderstood your reply but did you get that I am only using constFloat specifically where I need to handle a const pointer and no where else?

    glue code could slow things down, an extra call per call when there could be the need to retrieve several hundred const pointers per drawn frame doesn’t seem ideal either…

    Do you see any issues with my solution (given that yes it is a tad ungraceful) ?

    #4988

    codifies
    Participant

    @abakobo

    lol – now this server isn’t taking 30 seconds to serve a page, I managed to use the search…

    seems I’m not the only one to “discover” the

    Struct constFloat = “const float”

    hack!

    #5026

    abakobo
    Participant

    Here some working code (with an int). But you probably already get it.. or may be this is not relevant at all but I wanted to play with the ‘cast’ solution for const ptr. And this is nothing official, so could be deprecated? Never saw it in the docs..

    .monkey2

    [/crayon]

    facto_c.h:

    [/crayon]
    #5027

    codifies
    Participant

    yeah – thanks, that is basically what I’m doing and checking the generated code afaict its doing what I’d have done using C

    I assume you realise you don’t need the ci pointer, ie

    looking at a different example

    gets you

    Which looks sane to me!

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

You must be logged in to reply to this topic.