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
7 months, 1 week ago.
-
AuthorPosts
-
September 7, 2018 at 1:23 am #15382
I have a structure with a CString field (which is defined in some external C code)
It looks something like this:Monkey12345ExternStruct ModuleDefField m_name:CStringEndAnd 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:Monkey12Local m := New ModuleDefm.m_name = "hello"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 referenceExample:
Monkey12345678910Function Main()Local t := New Testt.str = "blah"EndStruct TestField str:CStringEndSeptember 7, 2018 at 1:19 pm #15390CString 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.
September 7, 2018 at 4:59 pm #15392Here 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
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#Import "<std>"#Import "<libc>"#Import "CStringStruct.h"#Import "CStringStruct.cpp"Using std..Using libc..ExternStruct SomestructField someName:char_t PtrMethod New(name:CString)Method New()Method Destroy() Extension="delete"Method SetName(name:CString)Method GetName:char_t Ptr()EndPublicFunction Main()Print "monkey2 extern test"Print ""Local stru:Somestructstru=New Somestruct ("John")Print String.FromCString(stru.GetName())Print String.FromCString(stru.someName)Local newName:="Chuck"Local mydata:=New DataBuffer( newName.CStringLength+1 )newName.ToCString( mydata.Data,mydata.Length )stru.someName=Cast <char_t Ptr> (mydata.Data) 'leaking memory here tooPrint String.FromCString(stru.GetName())End.h
C++1234567891011121314151617181920212223#ifndef CSTRINGSTRUCT_H#define CSTRINGSTRUCT_Hstruct Somestruct{public:char* someName;Somestruct(char* name);Somestruct();~Somestruct(); // destructorvoid SetName(char* name);char* GetName();};#endif.cpp
C++12345678910111213141516171819202122232425262728293031#include "CStringStruct.h"#include <iostream>// constructorSomestruct::Somestruct(char* name){SetName(name);}Somestruct::Somestruct(){char* name="unnamed";SetName(name);}Somestruct::~Somestruct(){// clean your memory}// member functionvoid Somestruct::SetName(char* name){someName = new char[ sizeof(name) + 1 ] ;strcpy( someName, name ) ;}char* Somestruct::GetName(){return this->someName;}Attachments:
-
AuthorPosts
You must be logged in to reply to this topic.