Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Strings (newbie question)

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Jan 2008 20:43
I have 2 string related questions:

1. What is the best way to deal with strings considering that I need to be able to add several strings together?

At the moment I'm doing the following whenever I want to use the dbPrint command:


Is that a good way of doing it, or is there a better/simpler way of doing it?

2. How do I convert integers into standard strings?
tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 17th Jan 2008 23:01
One way is to use more powerful string library, like ATLString for example:



You can do much more things and easily with this class.

Converting anything to string is possible using a method of the CAtlString or using a standard function:




See documentation for both both the function and the class. Also, you can use GDK function dbStr which converts integer to a string.
SynReaper
16
Years of Service
User Offline
Joined: 15th Jan 2008
Location:
Posted: 17th Jan 2008 23:18
The GDK has a built-in function for handling the conversion which is much faster to code:

SomeStringVar = dbStr ( iValue );

I have not thoroughly tested this function yet, but it will definitely return a string from an integer iValue.


-- 010100110111100101101110010100100110010101100001011100000110010101110010
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 18th Jan 2008 08:42
Quote: "One way is to use more powerful string library, like ATLString"


Where can I get my hands on this library - I had a quick google and couldn't find a download link for it.
tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 18th Jan 2008 09:14
Quote: "Where can I get my hands on this library - I had a quick google and couldn't find a download link for it."


It should be integral part of Visual Studio, so you basically only need to add the #include <atlstr.h> to your source code and then open project properties and in General tab set "Use of ATL" to "Using ATL in Static Library".

Unfortunately, I'm not sure if you have ATL since I checked my version of VC++ (VC++2008 Express edition) and it does not contain it, so probably you need full Visual Studio Express or VS Pro (I always use full installation of Pro version where it is)? Not sure, so try to check that out; if it's there, you would find a folder "atlmfc" in VSInstallDir\VC\
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 18th Jan 2008 13:41 Edited at: 18th Jan 2008 13:46
I think I need VS Pro, and that costs a fair bit I'll be sticking to standard strings then for now

However, I have discovered a much simpler way of converting strings to char.. simply using the .copy function:

tparkin
17
Years of Service
User Offline
Joined: 28th Mar 2007
Location:
Posted: 18th Jan 2008 14:50 Edited at: 18th Jan 2008 14:52
Hi,

The STL string class is just as powerful as the ATL string class. The STL string class (like all other STL classes) is integrated into all versions of Visual Studio.

It looks like you're already using it, but for those reading this thread who aren't:



For more information please visit:
http://www.sgi.com/tech/stl/basic_string.html
tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 18th Jan 2008 15:20
Quote: "The STL string class is just as powerful as the ATL string class."


Powerful - yes. Easy to use - no. That's why I suggested ATL string, but I didn't know it's not in the Express version.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 18th Jan 2008 15:51
Quote: "However, I have discovered a much simpler way of converting strings to char.. simply using the .copy function"

And wouldn't it be simpler still to just use the c_str() member of the string class as before?

tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 18th Jan 2008 16:15
Quote: "And wouldn't it be simpler still to just use the c_str() member of the string class as before?"


c_str() returns constant pointer, but functions like dbPrint expects non-constant pointer, so it can't be used directly (which is awful) .. it's not a problem of STL string class, that's again an amazing primitiveness of the GDK code :/
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 18th Jan 2008 16:22
Quote: "c_str() returns constant pointer, but functions like dbPrint expects non-constant pointer, so it can't be used directly (which is awful)"

Then typecast it:



Pixel Perfect
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: UK
Posted: 18th Jan 2008 16:24
Exactly, that's all it needs. That's the beauty of C

No matter how good your code is, someone will improve on it
tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 18th Jan 2008 16:41
Right, I can not argue in that, but it's not generally a good idea/habit to typecasting and pasting const pointers to a non-cost argument of a function which can potentially change it. (Sure, we can suppose that Print function won't change a string, still...)
Pixel Perfect
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: UK
Posted: 18th Jan 2008 16:59
Agreed, casting always has to be used with caution. But it's a powerful tool when used sensibly, and the benefit of using String classes over the standard C string handling has got to be worth it, especially where beginners are concerned as it avoids one of the greatest bug inducing areas for the un-initiated!

No matter how good your code is, someone will improve on it
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 18th Jan 2008 17:12 Edited at: 18th Jan 2008 17:12
Quote: "dbPrint((char*) myString.c_str());"


Damn, I wish I'd known how to do that before Note to self: read typecasting tutorial.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 18th Jan 2008 18:12 Edited at: 18th Jan 2008 18:20
Quote: "Right, I can not argue in that, but it's not generally a good idea/habit to typecasting and pasting const pointers to a non-cost argument of a function which can potentially change it."

Then call it bad practice that dbPrint doesn't take a const pointer.

Besides, regardless of const-ness, a function should not modify an input buffer unless it is documented that it does so. If it needs to modify it for its own internal use, it should make its own copy of it first.

At least, in my experience.

tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 18th Jan 2008 20:06
Quote: "Then call it bad practice that dbPrint doesn't take a const pointer."


That I stated before. There's a lot of uglyness in GDK

Quote: "Besides, regardless of const-ness, a function should not modify an input buffer unless it is documented that it does so. If it needs to modify it for its own internal use, it should make its own copy of it first."


True, indeed. On the other side - GDK docs lacks info.
Still you can pretty much assume what a function "SetPosition( const CVector * )" does without reading doc, but for a "SetPosition( CVector * )" I'm always unsure...

Login to post a reply

Server time is: 2024-09-29 09:16:11
Your offset time is: 2024-09-29 09:16:11