Author Topic: C++ Question  (Read 15974 times)

Meanie

  • Hero Member
  • *****
  • Posts: 2803
    • View Profile
    • http://www.users.on.net/~hluc/myZaurus/
C++ Question
« Reply #15 on: June 09, 2007, 04:04:16 am »
Quote
Quote
Quote
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.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=162874\"][{POST_SNAPBACK}][/a][/div]


sounds like QString is what you want
[div align=\"right\"][a href=\"index.php?act=findpost&pid=162881\"][{POST_SNAPBACK}][/a][/div]
I'm not using QT. Is that an issue?

Is it possible to just use either strings or character arrays?
[div align=\"right\"][a href=\"index.php?act=findpost&pid=162882\"][{POST_SNAPBACK}][/a][/div]

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...
SL-C3000 - pdaXii13 build5.4.9 (based on pdaXrom beta3) / SL-C3100 - Sharp ROM 1.02 JP (heavily customised)
Netgear MA701 CF, SanDisk ConnectPlus CF, Socket Bluetooth CF, 4GB Kingston CF,  4GB pqi SD, 4GB ChoiceOnly SD, 2GB SanDisk SD USB Plus, 1GB SanDisk USB Plus, 1GB Transcend SD, 2GB SanDisk MicroSD with SD adaptor, Piel Frama Leather Case, GoldX 5-in-1 USB cable, USB hub, USB mouse, USB keyboard, USB ethernet, USB HDD, many other USB accessories...
(Zaurus SL-C3000 owner since March 14. 2005, Zaurus SL-C3100 owner since September 21. 2005)
http://members.iinet.net.au/~wyso/myZaurus - zBook3K

pelrun

  • Sr. Member
  • ****
  • Posts: 366
    • View Profile
    • http://
C++ Question
« Reply #16 on: June 10, 2007, 09:53:23 am »
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;"
« Last Edit: June 10, 2007, 09:56:06 am by pelrun »
SL-C3100 with usb power mod running debian eabi
pdaXii13 still on the NAND for dualbooting
16GB SDHC! (a new one, after I sat on the old one and it went kaput)
D-Link 660 wifi, Socket Bluetooth rev H
External 9800mAh LiIon battery and slimline dvd drive
Homebrew microphone and remote

Snappy

  • Hero Member
  • *****
  • Posts: 793
    • View Profile
    • http://
C++ Question
« Reply #17 on: June 11, 2007, 12:51:50 am »
Quote
Quote
Quote
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.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=162874\"][{POST_SNAPBACK}][/a][/div]


sounds like QString is what you want
[div align=\"right\"][a href=\"index.php?act=findpost&pid=162881\"][{POST_SNAPBACK}][/a][/div]
I'm not using QT. Is that an issue?

Is it possible to just use either strings or character arrays?
[div align=\"right\"][a href=\"index.php?act=findpost&pid=162882\"][{POST_SNAPBACK}][/a][/div]

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.
Snappy!
------------------------
Akita (Daily use)
<span style='font-size:8pt;line-height:100%'>SL-C1000 with Cacko 1.23 full
Accessories: Wifi XI-825 CF  |  16GB (6) SDHC (Transcend) | 1GB 80x CF (RiDATA PRO-2) </span>

Collie (Sandbox)
<span style='font-size:8pt;line-height:100%'>SL-5500 with OZ/GPE 3.5.4.1 alpha3 build 2006-04-27
Accessories: 512MB A-Data SD | 64MB Toshiba SD</span>

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
C++ Question
« Reply #18 on: June 13, 2007, 11:28:02 pm »
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.
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
C++ Question
« Reply #19 on: June 14, 2007, 11:40:59 am »
Never mind, I worked around it.
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo