![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Convert an AnsiString to a char *Answer:Use the c_str() member function of AnsiString. Here are some examples. // Passing an AnsiString to a message box AnsiString str = "Hello Dr. Crane"; Application->MessageBox(str.c_str(), "AnsiString to char *", MB_OK); // Passing an AnsiString to an API function ::DrawText(Canvas->Handle, str.c_str(), str.Length(), rect, DT_VCENTER); Note 1: c_str is an inline member function of the AnsiString class. The code for c_str() reveals that the function returns the pointer to AnsiString's internal char * buffer. // Data is a char * member of AnsiString char* __fastcall c_str() const {return (Data)? Data: "";} This code is equivalent to: char* __fastcall AnsiString::c_str() const { if(Data != NULL) return (Data); else return ""; }Note 2: Because c_str returns a non-const pointer to the internal buffer, you can use c_str to modify an AnsiString variable. Here are some examples. // This example creates an AnsiString and fills it with zeros AnsiString str; str.SetLength(50); // allocate space for 50 characters memset (str.c_str(), 0,50); // fill buffer with zeroes // This example passes an AnsiString to an API function that wants to // write to the string. AnsiString str; DWORD size = 255; str.SetLength(size +1); // 255 characters plus null term GetComputerName(str.c_str(), &size) ; // call the API Get functions Note that just because you can write to the pointer returned by c_str(), this does not mean that you actually should. Modifying the internal data of an object is usually a bad idea. In fact, the std::string class does not allow you to write to the pointer returned by its c_str member function. Some people believe that AnsiString should also forbid it. Try to avoid writing to the c_str() buffer if at all possible Also keep in mind that when you write to c_str(), that the value returned from the Length member function will probably not equal the string length. It will equal the value that you passed to SetLength. You must also be careful not to overrun the buffer. Note 3: Note 1 shows that c_str is a constant member function of the AnsiString class. This means that you can call c_str on constant AnsiString variables. C++ does not allow you to call non-const functions on constant objects. The following code would not compile if Borland had omitted the const keyword in the declaration of the c_str method. const AnsiString str = "Hello Dr Crane"; Application->MessageBox(str.c_str(), "",MB_OK); // error, calling non-const // method on const object Note 4: You will almost always call the c_str function using the dot "." notation rather than the pointer notation "->". This also applies to AnsiString properties of VCL components. When a VCL component has an AnsiString property, the Get method for the property returns a new AnsiString object by value. Since the object is returned by value, you dereference its member functions using the dot notation. | ||||||
All rights reserved. |