Jump to content
  • Advertisement

Archived

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

xjussix

std::string, can I do this?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!