![]() ![]() |
Jun 9 2007, 12:04 AM
Post
#16
|
|
![]() Group: Members Posts: 2,808 Joined: 21-March 05 From: Sydney, Australia Member No.: 6,686 |
QUOTE(Capn_Fish @ Jun 9 2007, 12:57 PM) QUOTE(Meanie @ Jun 8 2007, 09:50 PM) QUOTE(Capn_Fish @ Jun 9 2007, 11:13 AM) Is there any way to only use one string-like variable (char[xyz] or string) in my code? Some things complain if I use a char[xyz], but not a string, others work the opposite way, and it would be easier to just use one type instead of converting back and forth. Thanks. sounds like QString is what you want I'm not using QT. Is that an issue? Is it possible to just use either strings or character arrays? either convert between them, or design your apps better to just use only methods that supports either one of them only. alternatively, use a easier to use framework which has already solved this problem for you... |
|
|
|
Jun 10 2007, 05:53 AM
Post
#17
|
|
|
Group: Members Posts: 369 Joined: 6-September 04 From: Brisbane, Australia Member No.: 4,488 |
Use std::string wherever you can (and pass const/non-const references to all your functions), it will make your life much easier. But there will be cases where you need to convert, generally for third-party library functions.
Conversion is pretty darn trivial, though. 1) .c_str() to get a const pointer to a std::string, you can use that straight in the function call e.g. someFunction(myString.c_str()); 2) create an old style char buffer in local scope for functions which modify their contents, then assign the buffer to the std::string you're using in the rest of your code. std::string is smart enough to understand "myStdString=myCString;" |
|
|
|
Jun 10 2007, 08:51 PM
Post
#18
|
|
|
Group: Members Posts: 793 Joined: 28-November 04 From: NM, US, sometimes Asia Member No.: 5,633 |
QUOTE(Capn_Fish @ Jun 9 2007, 10:57 AM) QUOTE(Meanie @ Jun 8 2007, 09:50 PM) QUOTE(Capn_Fish @ Jun 9 2007, 11:13 AM) Is there any way to only use one string-like variable (char[xyz] or string) in my code? Some things complain if I use a char[xyz], but not a string, others work the opposite way, and it would be easier to just use one type instead of converting back and forth. Thanks. sounds like QString is what you want I'm not using QT. Is that an issue? Is it possible to just use either strings or character arrays? I guess there is no easy way out but to convert them back and forth. Not too familiar with the string definition in QT3, but there might be some cast members that return a pointer from string that can be used as a char pointer in some functions. Using MFC (Microsoft Foundation Classes) as an example, CString sMyText; char szMyText[100]; char * lpszMyText; sMyText = "Some data here"; lpszMyText = sMyText.GetBuffer(sMyText.GetLength()); // Use lpszMyText strcpy(szMyText, lpszMyText); lpszMyText = NULL; // Not required but good practise to null the pointers // Release buffer pointer sMyText.ReleaseBuffer(); Hope this make sense. |
|
|
|
Jun 13 2007, 07:28 PM
Post
#19
|
|
![]() Group: Members Posts: 2,350 Joined: 30-July 06 Member No.: 10,575 |
Slowly working around my problem, but I have another question:
There is a clear() function to strings. Is there a similar function for character arrays? Sorry if that's a really dumb question, but I'm still new to C++. Thanks in advance. |
|
|
|
Jun 14 2007, 07:40 AM
Post
#20
|
|
![]() Group: Members Posts: 2,350 Joined: 30-July 06 Member No.: 10,575 |
Never mind, I worked around it.
|
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 24th May 2013 - 03:08 AM |