c_str in Delphi

Started by
7 comments, last by Afr0m@n 18 years, 10 months ago
I've stumbled across a function in Doom3 that does nothing but to "return name.c_str". Scanning through Google, the Delphi manual and the Visual Studio manual gives me no useful information whatsoever, except for a brief Microsoft explanation of the function itself which basically gives me squat except for confirming that the function c_str is not created internally by id. What I need to know is if there's a similar function in Delphi, and if not, where I can look at the source for the c_str function and convert it to Delphi. Thanks in advance!
_______________________Afr0Games
Advertisement
Perhaps you could actually mention what class "name" is, considering there are several "c_str" methods depending on whose library you are using.
sounds to me like the STL string c_str() method, which returns a C string

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringcstr.asp
const char * std::string::c_str()

AFAIK, it returns a constant pointer to the internal character buffer.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
So the question is not: what does c_str do, but how did you get your hands on an STL-type string from within Delphi :)

I think you better post some more of that code-fragment you are trying to translate here. Unless you are using string-classes (as opposed to basic string types) in your Delphi project too, you probably don't need it... at all.
Quote:Original post by pauljan
So the question is not: what does c_str do, but how did you get your hands on an STL-type string from within Delphi :)

Uhhh.. He mentioned finding the function in the Doom3 source code, which is presumably in C++/ASM, and wanting to reproduce it's functionality in Delphi, did he not? I didn't see that on my first look either.

Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Well.. er.. I don't know if it's going to help, but here's the original function declaration, in a class called idFile_Memory:

virtual const char *	GetName( void ) { return name.c_str(); }


name is a variable of type idStr (don't know what idStr is yet, and I can't figure it out either, because I have Visual Studio 6, and the project files are for Visual Studio .NET) in the private section of the class. I'd be delighted to know if there's some sort of workaround. If not, my guess is that I'm going to have to port the entire c_str function:\
_______________________Afr0Games
Not necessarily. The point I was trying to make is: name is some sort of string object (instance of a custom string class, as opposed to a simple type), it's up to you wether you want to implement it that way or not.

So here is your two options:

1. Make "name" a simple Delphi string. That would leave c_str redundant, so GetName would simply return name. Assuming you are in a class here, you'll probably want to make it a property Name returning fName. Unless they are doing very tricky things with that string object, I think this would be your best bet. Delphi strings support is pretty good.

2. Stick to the same design as the code you are trying to port, and implement a string class that has all the usual string functions (i.e. the string functions that are used in the doom3 code in particular) as class members. So something like:

type  TString = class( TObject )  private    fValue: String;  public   function c_str: String;   end;...function TString.c_str: String;begin  result := fValue;end;


Then again, if you would like the code to be more Delphi-style you probably would want to kick out the c_str function, and replace it with a default property called Value.
Thanks for the reply:) I think I'll stick to the original design, but I'll change it if it proves neccessary:)
_______________________Afr0Games

This topic is closed to new replies.

Advertisement