Setting CString in a Struct

About Monkey 2 Forums Monkey 2 Programming Help Setting CString in a Struct

This topic contains 2 replies, has 2 voices, and was last updated by  abakobo 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #15382

    Hezkore
    Participant

    I have a structure with a CString field (which is defined in some external C code)
    It looks something like this:

    And I need to set the m_name field to something at start.
    But since it’s a CString it means that it’s a constant and can’t be changed once initialized.
    So simply doing:

    Will results in an error Value 'm.m_name' is not assignable
    So how would I go about changing that field?

    I’ve tried cheating and renaming the field to m_name:String but I get the error error C2440: '=': cannot convert from 'bbString' to 'const char *' since the external source wants a CString.
    Same error if I do m_name:String Ptr
    If I make the field a CString Ptr and pass it a Varptr to a CString variable that holds the name I get Value 'name' is not a valid variable reference

    Example:

    #15390

    abakobo
    Participant

    CString is a convenience type to be used for external funcs/methods arguments. It will convert your string to a valid external CString automaticaly while passing it!

    If you really need to manipulate a CString inside Monkey2, you can do it with libc.char_t Ptr, and String.ToCString or String.FromCString. But you’ll be playing with a pure C style Ptr and can crash your app if you are not managing your memory correctly.

    #15392

    abakobo
    Participant

    Here is a running example with c++ code included. I’m not using any const stuff here, which could bring (a lot of?) trouble.

    !!! the C++ code is awful and WILL leak. Do not use it for serious work.

    It shows the use of CString thru the C++ constructor and also a direct modification of the char* field

    .monkey2

    .h

    .cpp

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

You must be logged in to reply to this topic.