#### Archived

This topic is now archived and is closed to further replies.

# std::string, can I do this?

This topic is 5650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

typedef std::string String;
Is that "legal"?

##### Share on other sites
Sure it is, C++ is case sensitive.

##### Share on other sites
Well, in that case then what should I do in order to be able to use String anywhere in my game code? I don''t want to write std::string every time.

Or should I do this:
#include <string>using std::string;

I hope you understand what I mean..

##### Share on other sites

#if !defined ( __MYSTRING__ )

#include <string>

typedef std :: string String;

#endif

....and then include this little file whenever you want to use string. OR (better), include this in a general "types" header.

##### Share on other sites
if you want to use your own string class for your game SpaceManSpiff it might be called 'string' and be defined in the sms namespace

spiffstring.h

    #ifndef SPIFFSTRING_H#define SPIFFSTRING_H#include <string>namespace sms {typedef std::string string;}#endif

then anywhere you want to use your string you can include spiffstring.h and either say sms::string or using sms::string;

if later you choose to replace the std::string with something else (which uses the same syntax) you can change your typedef or grow it into your own class.

so, you don't want to do a lot of typing?

well the best thing is, in header files, to put sms::string. it doesn't take that long. you should never put 'using' in a header.

in cpp files put 'using sms::string' either within the function that uses it or at the top of the file if there are a lot of functions using it.

then you can go ahead and refer to it as 'string'.

[edited by - petewood on December 3, 2002 6:36:52 AM]

##### Share on other sites
quote:
Original post by Robbo

#if !defined ( __MYSTRING__ )

#include <string>

typedef std :: string String;

#endif

....and then include this little file whenever you want to use string. OR (better), include this in a general "types" header.

I think this is the way I''m going to go, it sounds like a very neat way of doing it. Thanks a lot!

• 9
• 23
• 10
• 19