custom string class

This is a discussion on custom string class within the C++ Programming forums, part of the General Programming Boards category; I'm building a custom string class, and I'm wondering what operators I should overload, and what other functions should I ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    custom string class

    I'm building a custom string class, and I'm wondering what operators I should overload, and what other functions should I implement? I already have overloaded =, +, +=, >>, <<, >, <, ==, >=, <=, and !=. I also have functions to return the length of the string, both with and without the null terminator. So what else should I use? I plan to bring in functions such as insertion, erasing, replacing, and subscription. Thanks for ideas / suggestions !
    Do not make direct eye contact with me.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    - copy constructor
    - reference counting
    - C-string access
    - tokenizing
    - printf style formatting
    - searching
    - uppercase/lowercase,
    - case insensitive comparisons
    - custom buffer management (capacity(), resize(), etc...)
    - wide character support (wchar_t) (via templates?)

    gg

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Thanks for ideas / suggestions
    Any useful feature that cannot be separated from the string class. You'll notice that std::string is bloated because this was not done. We suffer because of poor design decisions.

    >- copy constructor
    Yes.

    >- reference counting
    Not necessary, but useful.

    >- C-string access
    Do you mean like c_str(), or something else?

    >- tokenizing
    Why should this be a part of the string class? Tokenizing is altogether separate from internal string operations.

    >- printf style formatting
    For what exactly?

    >- uppercase/lowercase
    A container shouldn't need to worry about the data that it holds. Also, what about locale specific character sets? How can you as the designer of a class know what upper and lower case means to every single client? Better to separate character traits from the string as well.

    >- case insensitive comparisons
    Once again, there are issues concerning what case is. If you separated character traits from the string class to begin with, a string can be created with a locale specific case insensitive character type. This solution is cleaner and far more robust.

    - custom buffer management (capacity(), resize(), etc...)
    This might actually be better in a high performance offshoot of the string class lest it cause clutter, but that is a borderline decision that depends on how the general class is to be used, generally.

    >- wide character support (wchar_t) (via templates?)
    Once again, separating character traits from the string solves your problem quite nicely.
    My best code is written with the delete key.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A substring function would probably be useful. Other than that, keep it simple.

    If you need a lot of functions to manipulate strings, consider separating them out and just using your set of primitive operations on the string.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Prelude, would you be able to explain what an "locale specific case insensitive character type." is?

    thanks, subdene.
    Be a leader and not a follower.

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    - copy constructor
    Got it.
    - reference counting
    Not quite sure what that is - can you refresh me ?
    - C-string access
    I have operator char *

    - tokenizing
    - printf style formatting
    - searching
    - uppercase/lowercase,
    - case insensitive comparisons
    Basically same answer as prelude
    - custom buffer management (capacity(), resize(), etc...)
    Possibly, that might be an option.
    - wide character support (wchar_t) (via templates?)
    Same answer as preludes
    Do not make direct eye contact with me.

  7. #7
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Any useful feature that cannot be separated from the string class. You'll notice that std::string is bloated because this was not done. We suffer because of poor design decisions.
    I know exactly what you mean. What would you suggest taking out of std::string? Maybe my class will turn out better than Standard C++'s is .


    By the way, would a operator-(string s) be good to remove all instances of s from the string? Thanks again !
    Do not make direct eye contact with me.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Prelude, would you be able to explain what an "locale specific case insensitive character type." is?
    Basically, a call to setlocale and normal tests with toupper and/or tolower.
    My best code is written with the delete key.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What would you suggest taking out of std::string?
    http://www.gotw.ca/gotw/084.htm

    >would a operator-(string s) be good to remove all instances of s from the string?
    No. I don't know of any previous use in such a way, so you would only confuse people.
    My best code is written with the delete key.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Ugh, I feel like I'm spamming, seeing a new question and posting again to answer it. A pity edit doesn't allow you to easily cut/paste from the rest of the thread.

    - reference counting
    Not quite sure what that is - can you refresh me ?
    The string is only destroyed when there are no more references to it. This gives you opportunities for optimization like having objects with the same string to point to a single object. Then if you need to modify one of them but not any of the others, you can give that reference its own copy of the data, this is called copy-on-write semantics. Look up Effective C++ and More Effective C++ for detail.
    - C-string access
    I have operator char *
    Beware, this conversion may be performed when you don't expect it. There is a good reason the standard library chose to use c_str() instead of operator const char*().
    My best code is written with the delete key.

  11. #11
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by Prelude
    >would a operator-(string s) be good to remove all instances of s from the string?
    No. I don't know of any previous use in such a way, so you would only confuse people.
    I figured as much, thanks. Can you explain reference counting? It sounds familiar, but I can't place it.

    EDIT: Nevermind, thanks for spamming .
    Do not make direct eye contact with me.

  12. #12
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Beware, this conversion may be performed when you don't expect it. There is a good reason the standard library chose to use c_str() instead of operator const char*().
    I know, but it also has many advantages along with it.
    Do not make direct eye contact with me.

  13. #13
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Is this the Effective C++ you are talking about?
    Do not make direct eye contact with me.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I know, but it also has many advantages along with it.
    The advantages are quiet, which means the disadvantages are too. It is better to have disadvantages that slap you in the face instead of sneak behind you.

    >Is this the Effective C++ you are talking about?
    Yes.
    My best code is written with the delete key.

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    My advice would be to stay away from reference counting and copy on write. Both have issues and neither of them are efficient in multithreaded usage. If writing a string class try the small string optimisation first. Thats where your member is a union with two members. A fixed size charT array and a charT pointer. If the string has length less than some default value ( 16-32 bytes reasonable) it can be stored within the string objects char array. If the string is too long for that you instead write it into dynamic memory and set the charT* to point to it.This allows for less dynamic allocations and is much more multithread friendly and worth consideration before you start planning.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. [Linked Lists] Custom String Class Add Char problems
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2004, 10:15 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21