string or const char * in a function parameter list ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iu2

    string or const char * in a function parameter list ?

    Hi all,

    I find myselft wondering about the recommended way to pass strings to
    functions:

    int func(string s);

    or

    int func(const char *s);

    I usually pass a const char * since it's shorter referring to s
    instead of s.c_str() inside 'func'. In case some string manipulation
    need arises, I can always launch a string or a stringstrem insinde
    'func'.
    Is there a good reason to pass a 'string' instead of a const char * ?

    thanks
    iu2

  • Neelesh Bodas

    #2
    Re: string or const char * in a function parameter list ?

    On Aug 23, 12:08 pm, iu2 <isra...@elbit. co.ilwrote:
    Hi all,
    >
    I find myselft wondering about the recommended way to pass strings to
    functions:
    >
    int func(string s);
    >
    or
    >
    int func(const char *s);
    >
    I usually pass a const char * since it's shorter referring to s
    instead of s.c_str() inside 'func'. In case some string manipulation
    need arises, I can always launch a string or a stringstrem insinde
    'func'.
    Is there a good reason to pass a 'string' instead of a const char * ?
    >

    Using string class is much much much safer than using bare character
    pointers.



    -N

    Comment

    • john.papaioannou@gmail.com

      #3
      Re: string or const char * in a function parameter list ?

      First of all, you should not pass a string object by value most of the
      time. If your function is not going to modify the string then you
      should pass a const string& to avoid a copy of the object being made
      (if you are passing a const char* as in your other example it means
      that you are definitely not going to modify the string).

      That said, if you have to ask the question then you should be using a
      string of some sort instead of a const char*. It's much safer this
      way.

      Of course, if the program is really simple or you have a need for
      speed, then char* can be a better choice.

      Comment

      • tragomaskhalos

        #4
        Re: string or const char * in a function parameter list ?

        On Aug 23, 8:08 am, iu2 <isra...@elbit. co.ilwrote:
        Hi all,
        >
        I find myselft wondering about the recommended way to pass strings to
        functions:
        >
        int func(string s);
        or
        int func(const char *s);
        >
        Firstly, do not use the first form: prefer (const string&).
        With that fix in place, I would say it depends on a number of factors:
        1. If func needs the facilities of std::string, provide the const
        string& form
        2. If you envisage that a lot of calling code will use C-style
        strings, provide
        the const char* form.

        Finally, it's easy enough to provide both and delegate; where you put
        the
        'meat' of the processing depends on what func actually does: Either:

        int func (const string& s) { .... }
        int func (const char* s) { return func(string(s)) ; }

        Or

        int func (const string& s) { return func(s.c_str()) ; }
        int func (const char* s) { ... }

        Cheers.

        Comment

        Working...