Subclassing string class

Discussion in 'C++' started by Ray, Jul 15, 2008.

  1. Ray

    Ray Guest

    Hi all,

    I am thinking of subclassing the standard string class so I can do
    something like:

    mystring str;
    ....
    str.toLower ();

    A quick search on this newsgroup has found messages by others
    suggesting that subclassing a standard class is a bad idea, especially
    for beginners...

    Can someone suggest what is the recommended way to add functionality
    to (say) the string class? Actually, I would like to do other things
    to strings, and toLower is just one of them. Is it better or is the
    only way to just perform a function call:

    toLower (string s)

    [Of course, there is a C function with the same name, so something
    similar to it.]

    Another way is to create a class with a private member variable "data"
    of type string I should add that I'm somewhat motivated by efficiency
    as these tasks with strings will be performed very often.

    Which of these two is recommended? Or is there a third that I have
    not thought of?

    Thank you!

    Ray
     
    Ray, Jul 15, 2008
    #1
    1. Advertising

  2. Ray wrote:

    > Can someone suggest what is the recommended way to add functionality
    > to (say) the string class? Actually, I would like to do other things
    > to strings, and toLower is just one of them.


    Why not use the String Algorithms library in Boost?
    http://www.boost.org/doc/libs/1_35_0/doc/html/string_algo.html

    It may already offer most of the functionality you need, including case
    conversion algorithms.

    std::string source = "FooBar";
    boost::algorithm::to_lower(source);
    std::cout << source; //prints "foobar"

    Note how the library does *not* derive from std::string but uses
    non-member functions that take std::string parameters.


    --
    Christian Hackl
     
    Christian Hackl, Jul 15, 2008
    #2
    1. Advertising

  3. Ray

    Ray Guest

    Hi Victor/Alf,

    Thank you both for your replies. Actually, I had gotten a little
    further than what I posted and stumbled on exactly what you two are
    now discussing...

    I derived a class mystring (sorry, I won't say "subclass" :) ) from
    string and was trying to do:

    mystring foo
    ....
    foo.length ();

    I didn't think about casting...but I thought somehow that should have
    worked. Obviously, not... Thanks for the two suggestions and the
    pros/cons of each!

    Ray



    On Jul 16, 6:25 am, "Alf P. Steinbach" <> wrote:
    > * Victor Bazarov:
    > > Alf P. Steinbach wrote:
    > >> * Victor Bazarov:

    >
    > > class M {};
    > > void foo(M&);
    > > class L { public: operator M&(); };

    >
    > > versus

    >
    > > class M { public: foo(); };
    > > class L { public: operator M&(); };

    >
    > > With the former you can have

    >
    > > L l;
    > > foo(l);

    >
    > > but with the latter you can't do

    >
    > > L l;
    > > l.foo();

    >
    > Reference operators are bad 'uns generally, but for latter you could just
    > static_cast<M&>(l).foo(), and even static_cast<M&>(L()).foo().
    >
    > But I think the above misses the point, by introducing that conversion to reference.
     
    Ray, Jul 16, 2008
    #3
  4. Ray

    Ray Guest

    Hi Christian,

    Thanks for this -- I didn't know that the Boost library had string
    algorithms. I should have thought that something like "tolower" would
    be there. I think I'll take this option; I'm obviously too new to
    continue going down the current path. :) Thank you!

    Ray



    On Jul 16, 7:13 am, Christian Hackl <> wrote:
    > Ray wrote:
    > > Can someone suggest what is the recommended way to add functionality
    > > to (say) the string class? Actually, I would like to do other things
    > > to strings, and toLower is just one of them.

    >
    > Why not use the String Algorithms library in Boost?http://www.boost.org/doc/libs/1_35_0/doc/html/string_algo.html
    >
    > It may already offer most of the functionality you need, including case
    > conversion algorithms.
    >
    > std::string source = "FooBar";
    > boost::algorithm::to_lower(source);
    > std::cout << source; //prints "foobar"
    >
    > Note how the library does *not* derive from std::string but uses
    > non-member functions that take std::string parameters.
    >
    > --
    > Christian Hackl
     
    Ray, Jul 16, 2008
    #4
    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. E11
    Replies:
    1
    Views:
    4,792
    Thomas Weidenfeller
    Oct 12, 2005
  2. Robin Becker

    Subclassing String in C

    Robin Becker, Nov 11, 2003, in forum: Python
    Replies:
    0
    Views:
    298
    Robin Becker
    Nov 11, 2003
  3. Fuzzyman

    My Experiences Subclassing String

    Fuzzyman, Jun 7, 2004, in forum: Python
    Replies:
    2
    Views:
    350
    Fuzzyman
    Jun 8, 2004
  4. petertwocakes

    Subclassing std::string confusion?

    petertwocakes, Nov 28, 2009, in forum: C++
    Replies:
    2
    Views:
    747
    petertwocakes
    Nov 28, 2009
  5. Replies:
    0
    Views:
    138
Loading...

Share This Page