Re: Cleverly extending the std::string class

Discussion in 'C++' started by James Kanze, Mar 19, 2009.

  1. James Kanze

    James Kanze Guest

    On Mar 19, 7:58 pm, Giuliano Bertoletti <> wrote:

    > just wondering if there's a clever way to extend the
    > functionality of std::string (without using boost).


    > Newer programming languages like python do have a lot of
    > functionality associated with strings, which may be handy in
    > C++.


    > For example lowercasing and uppercasing the string.
    > Extracting the last element by subscripting -1, or the last
    > say four chars using [-4:-1] are also desirable.


    > These functionalities could be accomodated by proper function
    > calls.


    Exactly.

    > If I derive a class from std::string I have all the
    > std::string member functions return the base class and not the
    > derived class.


    > So for example I would have to write:


    > std::string p = "test";
    > std::string q = ((MYSTRING)p).lower()


    Which would result in undefined behavior.

    > while I would prefer to work only with MYSTRING.


    > MYSTRING p;
    > MYSTRING q = p.lower();


    > but then if I call:


    > q = p.substr(0,4)


    > substr returns an std::string and not a MYSTRING.


    > Is this the wrong place where to use inheritance ?


    Yes. If you want a string class with different behavior, then
    write one. It's not an std::string (although it might use an
    std::string in its implementation). If you simply want
    functions which operate on a string, then there's nothing wrong
    with providing such functions; I do so (in the most recent
    versions of my library, in two forms: one which supposed UTF-8
    encoding, and the other which is based on the locale dependent
    single byte encoding).

    In either case, deriving from std::string is probably not a good
    solution.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 19, 2009
    #1
    1. Advertising

  2. James Kanze

    James Kanze Guest

    On Mar 20, 2:15 am, Victor Bazarov <> wrote:
    > James Kanze wrote:


    [...]
    > >> So for example I would have to write:


    > >> std::string p = "test";
    > >> std::string q = ((MYSTRING)p).lower()


    > > Which would result in undefined behavior.


    > Why?


    I was just about to answer "because the standard says so", but I
    notice that the conversion is to the concrete type, not to a
    reference to the type. In other words, you construct a new
    MYSTRING, and call lower() on it. Which is well defined.
    (Using a cast to trick the compiler into calling lower on an
    std::string wouldn't be.)

    It's interesting to note how the way the cast is written affects
    us psychologically. I saw a C style case, and so immediately
    thought in terms of what you can do with casts in C. I think
    most people would have written MYSTRING(p), instead of
    (MYSTRING)p; the former seems to have become the more or less
    standard way of creating a temporary object---probably because
    you can use the same syntax with different numbers of arguments.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 20, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,261
    Ivan Vecerina
    Mar 17, 2005
  2. Fei Liu
    Replies:
    9
    Views:
    431
  3. Jeffrey Walton
    Replies:
    10
    Views:
    927
    Mathias Gaunard
    Nov 26, 2006
  4. peter koch
    Replies:
    6
    Views:
    377
    James Kanze
    Mar 24, 2009
  5. Lars Schouw

    extending std::string

    Lars Schouw, Apr 7, 2010, in forum: C++
    Replies:
    9
    Views:
    1,428
    Lars Schouw
    Apr 11, 2010
Loading...

Share This Page