Customized String Class

Started by
13 comments, last by Rob Loach 20 years, 10 months ago
Perhaps this migh be helpful.

Click me

you''ll want to look at the apstring class

--{You fight like a dairy farmer!}

Advertisement
my book ObjectOrented Programming in C++ second edition, by Robert Lafore covers the creation of you own string class.
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
quote:Original post by Rob Loach
Thanks alot Oluseyi. I was also thinking of ways to make assignment and manipulation easier...
Take a look at Ecliptici''s suggestion; that''s a good assignment operator. Make sure to also overload the copy constructor.

quote:[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?
Lol. No, "semantics" means "meanings" in a sense, or usage. Pointer semantics deals with the way pointer operations are interpreted both by programmers and the compiler. For example, you can offset from a pointer:
pointer + 5; 
You can advance a pointer:
++pointer; 
You can dereference a pointer:
*pointer; 
You can subscript a pointer, which is equivalent to offsetting and dereferencing:
pointer[5]; // same as ''*(pointer + 5)'' 
There are a few other pointer operations as well: assignment of the pointer, decrementing the pointer, pointer comparisons (if two pointers point to parts of the same sequence and pointer_a < pointer_b, then *pointer_a must occur earlier in the sequence than *pointer_b) and so forth. Your iterator type must support these normal semantics. I''d recommend reading The Standard Template Library: Introduction''s section on the iterator concept and how it relates to pointers, as well as Strings in SGI STL when you''re a little more comfortable with your string class.

Good luck!
making your own string class is good for learning and if done well it can be much more useful then the std::string class. you can add functions like myString.ExtractQuote() or myString.RemoveSpaces(). A very useful one would be myString.GetNextWord().

to do this you would have to keep another variable withing the class that keeps track of the position your at within our string. an index. so if myString contained something like "this is a string" then caliing function myString.GetNextWord() would return "this" and it would increment the internal index to 4. so that the next time you call myString.GetNextWord() it would return "is"



:::: [ Triple Buffer V2.0 ] ::::
[size=2]aliak.net
quote:Original post by IFooBar
making your own string class is good for learning and if done well it can be much more useful then the std::string class. you can add functions like myString.ExtractQuote() or myString.RemoveSpaces(). A very useful one would be myString.GetNextWord().
That''s silly. In fact, that''s bloat.

Such functions shouldn''t be part of the string class but instead utility functions that take strings as arguments.

quote:to do this you would have to keep another variable withing the class that keeps track of the position your at within our string.
Which is why that''s bloat; now you''re adding variables that aren''t really properties of the string or parts of its implementation, but "helpers" to functions that really shouldn''t be part of the class.

quote:so if myString contained something like "this is a string" then caliing function myString.GetNextWord() would return "this" and it would increment the internal index to 4. so that the next time you call myString.GetNextWord() it would return "is"
Take a look at strtok and how it works. Doesn''t need to be part of the string class; it could use an iterator within its own implementation:
mystring getNextWord( const mystring & sentence ){  static mystring::iterator position;  static mystring current;   if( sentence != current )  {    current = sentence;    position = sentence.begin();  }  mystring::iterator iter = position;  while( *iter != " " )    ++iter;  if( iter == current.end() )    return iter;  else  {    std::swap( position, iter );    return mystring(iter, (iter - position) );  }}
Don''t add features to code unnecessarily. It''s called feature creep.

This topic is closed to new replies.

Advertisement