Jump to content
  • Advertisement

Archived

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

Rob Loach

Customized String Class

This topic is 5479 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

I''m thinking of making my own string-like class/data type to simplify my life around strings. Has anyone done this before? Any advise about how I could/should do it?
Rob Loach Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore "The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - The Boondock Saints

Share this post


Link to post
Share on other sites
Advertisement
I''ll second that. If your main purpose is to simplify your life then use std::string. Writing your own string class can be a good learning experience but it''ll probably give you just as many headaches as using character arrays (aka C style strings) if not more.

Share this post


Link to post
Share on other sites
quote:
Original post by Invader X
Yes. std::string



I never really laugh out loud - but I did when I read this I don''t know why.

Share this post


Link to post
Share on other sites
Assuming you actually wanted to write a string class for learning purposes, consider the following:
  • Store the length of your string within the object; this allows length operations to be faster and more efficient than searching a character array for a null.


  • Define an iterator type with pointer semantics. Makes it compatible with several algorithms and programming techniques.


  • Whenever your string approaches capacity and needs to reallocate, allocate at least 1.5 times the current capacity - or allow the user to specify the ratio.

Just a few useful pointers.

Share this post


Link to post
Share on other sites
Use std::string if you can''t find a very compelling reason for not doing that.

Directly transalated from swedish:
Don''t go to the other side of the stream (almost funny that too for water.

Share this post


Link to post
Share on other sites
From your question it is easy to judge your knowledge, will this in mind I want you to not get afraid of Oluseyi''s suggestions, as I think some of them are too advanced for you.

It''s a good learning experience to create your own string class and I highly recomend you to do so.

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Assuming you actually wanted to write a string class for learning purposes, consider the following:
  • Store the length of your string within the object; this allows length operations to be faster and more efficient than searching a character array for a null.


  • Define an iterator type with pointer semantics. Makes it compatible with several algorithms and programming techniques.


  • Whenever your string approaches capacity and needs to reallocate, allocate at least 1.5 times the current capacity - or allow the user to specify the ratio.

Just a few useful pointers.


Thanks alot Oluseyi. I was also thinking of ways to make assignment and manipulation easier...

cString playerName = "Joey";   

I don't think that would work as that's assigning the whole class to the character array given. With what I have now, I have to go:
cString playerName;
playerName.set("Joey");
cout << "Player Name: " << playerName.get();


Does anyone know how to make it so that I could go: playerName = "Blah"; ?

[EDIT] And yup... I store the size of the string when it is re-assigned, and the size of the array is realocated whenever is comes close to its capacity, but I haven't learnt about pointer semantics yet. What does that mean? Advanced use of pointers?



Rob Loach
Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?"
- The Boondock Saints


[edited by - Rob Loach on June 19, 2003 2:12:00 PM]

Share this post


Link to post
Share on other sites

class cString
{
public:
cString& operator = ( const char* );


private:
char* mString;
int len;
};

cString& cString::operator = ( const char* str )
{
int nlen = strlen(str);
if(len < nlen)
{
if(mString)
delete [] mString;
mString = new char[nlen];
len = nlen;
}
strcpy(mString,str);
return *this;
}

[/CODE]

Something like this, maybe?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!