Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ string question

Name: Anonymous 2012-02-15 13:28

I've avoided C++ for a long time but after a few months of using two high level languages (PHP and Javascript), I can't productively rely on C's strings any more. So I have a question. Is there any way of using C's string's with old C functions like strcat() and strcpy(). Yeah, I'm aware of the C++ equivalents.


int main(void)
{
    string firstName = "John";
    string lastName = "Doe";
   
    //This doesn't compile.
    strcat(firstName, lastName.c_str());
    //This compiles but doesn't work.
    strcat(&firstName[0], lastName.c_str());
}


Note: I'm aware that firstName += lastName would work. Before migrating to C++ I have to find a way to get C++ strings to work with C functions. For example, in the Windows API there's a function called GetSystemDirectory(char * path, int size) which sets the variable 'path' with the path of the system directory. If possible I don't want to have to create C arrays to deal with functions like that. Thanks in advance.

Name: Anonymous 2012-02-15 13:45

>>1
First of all, using strcat as an example makes you look borderline retarded, and on the wrong side of the border, at that.

Second, for functions like GetSystemDirectory .c_str() will work fine.

If you want to use functions which expect a buffer, you have two options. You can be a good boy and malloc or statically allocate a buffer which you then assign/return as an std::string.

Or you can depend on the way all existing implementations work, and on the wording of C++11 standard which kind of more or less seems to make this legal from now on, and do
buffer.resize(1024, '\0');
hr = GetFullPathName(path.c_str(), buffer.size(), &buffer[0], NULL);


Though I must add that if despite of your alleged C background you couldn't understand what's the problem with your code then you probably are not born to be a programmer and should consider a career in sucking dicks instead.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List