Crystal Space
Welcome, Guest. Please login or register.
November 21, 2014, 12:08:14 am

Login with username, password and session length
Search:     Advanced search
9018 Posts in 2046 Topics by 9077 Members
Latest Member: Pwrsrg
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  Bug? Comparing an element of csStringArray with const char* doesn't work?
« previous next »
Pages: [1] Print
Author Topic: Bug? Comparing an element of csStringArray with const char* doesn't work?  (Read 2803 times)
mark
Full Member
***
Posts: 101


View Profile
« on: September 15, 2005, 01:54:58 pm »

I'm using a csStringArray and want to compare the strings it contains:

Code:
csStringArray datenArray;
datenArray.Push(csString("netcmd"));
if(datenArray[0]=="netcmd") {...}

But: The comparision inside the if-statement returns false!
I checked datenArray[0] with csPrintf and it's right.
The comparision only works after I initialized another csString with datenArray[0]:

Code:
csStringArray datenArray;
datenArray.Push(csString("netcmd"));
csString xxx = datenArray[0];
if(xxx=="netcmd") {...}
this works! Why?

I know that csStringArray::operator[] returns a csString& and csString has operator==(const char *Str) const so this must be a bug - or what?
Logged

Gentoo Linux ~x86, kernel 2.6.11-cko9 smp, gcc 3.4.4-r1, binutils 2.16.1, glibc 2.3.5 NPTL
CS+CEL Pseudo Stable 2005.09.03
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #1 on: September 15, 2005, 02:17:29 pm »

I'm using a csStringArray and want to compare the strings it contains:

Code:
csStringArray datenArray;
datenArray.Push(csString("netcmd"));
if(datenArray[0]=="netcmd") {...}

But: The comparision inside the if-statement returns false!
I checked datenArray[0] with csPrintf and it's right.
The comparision only works after I initialized another csString with datenArray[0]:

Code:
csStringArray datenArray;
datenArray.Push(csString("netcmd"));
csString xxx = datenArray[0];
if(xxx=="netcmd") {...}
this works! Why?

I know that csStringArray::operator[] returns a csString& and csString has operator==(const char *Str) const so this must be a bug - or what?


csStringArray::operator[] returns const char* and NOT csString&. So you can't use the == operator because that will simply compare pointers. You have to either assign to csString like you are doing or else use the strcmp() function.

Greetings,
Logged
mark
Full Member
***
Posts: 101


View Profile
« Reply #2 on: September 15, 2005, 02:43:31 pm »

nice to know grin
csArray<csString> is what I need in this case. Thanks for the answer.
Logged

Gentoo Linux ~x86, kernel 2.6.11-cko9 smp, gcc 3.4.4-r1, binutils 2.16.1, glibc 2.3.5 NPTL
CS+CEL Pseudo Stable 2005.09.03
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #3 on: September 15, 2005, 03:53:38 pm »

nice to know grin
csArray<csString> is what I need in this case. Thanks for the answer.

Why? Note that csArray<csString> will be less efficient in array handling than csStringArray. I would recommend csStringArray.

Greetings,
Logged
Administrator
Jr. Member
*****
Posts: 51


View Profile Email
« Reply #4 on: September 15, 2005, 06:37:05 pm »

Here I have to disagree with Jorrit Wink
Many times ease of use goes over efficiency, so I would say unless you find your string array to be any major performance or memory bottleneck, use csArray<csString> if you feel like doing it.

-M
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #5 on: September 15, 2005, 07:18:55 pm »

Perhaps but on the other hand csStringArray has a number of string-array specific utility functions which are not present in csArray<csString>. So in that sense using csStringArray is actually easier.

Greetings,
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 5.471 seconds with 17 queries.