Sign in to follow this  
Afr0m@n

c_str in Delphi

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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this