Passing string from D to c++

JS js.mdnq at gmail.com
Tue Jul 30 14:17:10 PDT 2013


On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:
> On Tuesday, 30 July 2013 at 20:02:51 UTC, Dicebot wrote:
>> On Tuesday, 30 July 2013 at 19:52:44 UTC, Milvakili wrote:
>>> I'm linking D with C++ lib.a file.  When the C++ function has 
>>> compatible data types I can call them from D.  But when I 
>>> changed the parameter to string I got bunch of errors.
>>>
>>> Data Type Compatibility table does not include strings.  Is 
>>> there a way of passing strings?
>>
>> http://dlang.org/phobos/std_string.html#.toStringz
>
>
> So I need to pass them as char*, I can not pass them as string?

You can't pass them as string because they have different 
representations. A C++ string is null terminated while a D string 
is not. If you pass a D string to C++ then it will not have the 
same length and give undesirable behavior.

If you really want to use strings, then wrap each C++ function 
with one that converts the string to a C string. If D used both 
pascal and C++ style strings as one(a waste of 1 extra byte per 
string) then life would be easier.




More information about the Digitalmars-d mailing list