replacing c_str with operator asterix

Discussion in 'C++' started by sqwirx, Dec 21, 2003.

  1. sqwirx

    sqwirx Guest

    I am using a String that has a member function c_str(). Because I
    think c_str() reduces the readability of the code and I don't want
    implicit conversions, I'm concidering to add a "char const *
    operator*()". When you look at String as a fancy char* smartpointer
    then it makes perfectly sense to dereference it with operator*.

    What you think of it?
    sqwirx, Dec 21, 2003
    #1
    1. Advertising

  2. sqwirx

    Cy Edmunds Guest

    "sqwirx" <> wrote in message
    news:...
    > I am using a String that has a member function c_str(). Because I
    > think c_str() reduces the readability of the code and I don't want
    > implicit conversions, I'm concidering to add a "char const *
    > operator*()". When you look at String as a fancy char* smartpointer
    > then it makes perfectly sense to dereference it with operator*.
    >
    > What you think of it?


    You can hire thousands of programmers who will immediately understand
    std::string::c_str(). Who is going to understand String::eek:perator * ()?

    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Dec 21, 2003
    #2
    1. Advertising

  3. "sqwirx" <> wrote in message
    news:...
    > I am using a String that has a member function c_str(). Because I
    > think c_str() reduces the readability of the code and I don't want
    > implicit conversions, I'm concidering to add a "char const *
    > operator*()". When you look at String as a fancy char* smartpointer
    > then it makes perfectly sense to dereference it with operator*.
    >
    > What you think of it?


    I agree with Cy, this not a good idea; even many alternative string classes
    have a c_str() member. Inventing your own conventions won't make your code
    clearer to other people. Unintuitive operator overloading only obfuscates
    your code. So personnaly I'd stick with the standard conventions.

    I have seen string classes with implicit conversion operators to const char*
    (e.g. MFC's CString class) so you could write:

    CString str = "Hello";
    const char* ptr = str;

    Those type conversion operators may seem appealing at first, but classes
    with type conversion operators can yield unexpected results. So I tend to
    avoid those to.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Dec 21, 2003
    #3
    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. Ericcson

    ? Why c_str() ?

    Ericcson, Jul 28, 2003, in forum: C++
    Replies:
    4
    Views:
    970
    Kevin Goodsell
    Aug 13, 2003
  2. klaus hoffmann
    Replies:
    2
    Views:
    351
    klaus hoffmann
    Aug 14, 2003
  3. Tony Murphy
    Replies:
    2
    Views:
    629
    Tony Murphy
    Nov 11, 2003
  4. Christopher Benson-Manica

    std::string c_str()

    Christopher Benson-Manica, Feb 17, 2004, in forum: C++
    Replies:
    1
    Views:
    2,956
    Gianni Mariani
    Feb 17, 2004
  5. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    259
    Chris Hohmann
    Apr 11, 2005
Loading...

Share This Page