# c_str in Delphi

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
Perhaps you could actually mention what class "name" is, considering there are several "c_str" methods depending on whose library you are using.

##### Share on other sites
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

##### Share on other sites
const char * std::string::c_str()

AFAIK, it returns a constant pointer to the internal character buffer.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by pauljanSo 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.

##### Share on other sites
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:\

##### Share on other sites
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.

##### Share on other sites
Thanks for the reply:) I think I'll stick to the original design, but I'll change it if it proves neccessary:)

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 13
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632927
• Total Posts
3009241
• ### Who's Online (See full list)

There are no registered users currently online

×