perferred way of converting const char * to const std::string &

Discussion in 'C++' started by pookiebearbottom@yahoo.com, Aug 9, 2006.

  1. Guest

    Just looking for opinion on which of the 3 methods below people use in
    their code when they convert a 'const char *' to a 'const std::string
    &'

    came across #3 in someone's code and I had to think for a sec. At
    first I read it as converting a 'const char *' to a 'std::string *'

    void f(const std::string &s)
    {
    std::cout << s.size() << "\n";
    }

    int main()
    {
    const char *c="sal";

    f(c); //1
    f(std::string(c)); //2
    f(static_cast<std::string>(c)); //3

    return 0;
    }
     
    , Aug 9, 2006
    #1
    1. Advertising

  2. wrote:
    > Just looking for opinion on which of the 3 methods below people use in
    > their code when they convert a 'const char *' to a 'const std::string
    > &'
    >
    > came across #3 in someone's code and I had to think for a sec. At
    > first I read it as converting a 'const char *' to a 'std::string *'
    >
    > void f(const std::string &s)
    > {
    > std::cout << s.size() << "\n";
    > }
    >
    > int main()
    > {
    > const char *c="sal";
    >
    > f(c); //1
    > f(std::string(c)); //2
    > f(static_cast<std::string>(c)); //3
    >
    > return 0;
    > }


    (1) is the most common.

    (2) is only needed when you have 'f' overloaded and must pick one with
    'std::string' as its argument.

    (3) is the same as (2), IMO.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 9, 2006
    #2
    1. Advertising

  3. Heinz Ozwirk Guest

    <> schrieb im Newsbeitrag
    news:...
    > Just looking for opinion on which of the 3 methods below people use in
    > their code when they convert a 'const char *' to a 'const std::string
    > &'
    >
    > came across #3 in someone's code and I had to think for a sec. At
    > first I read it as converting a 'const char *' to a 'std::string *'
    >
    > void f(const std::string &s)
    > {
    > std::cout << s.size() << "\n";
    > }
    >
    > int main()
    > {
    > const char *c="sal";
    >
    > f(c); //1
    > f(std::string(c)); //2
    > f(static_cast<std::string>(c)); //3
    >
    > return 0;
    > }


    If it works without a cast, you shouldn't use a cast. So (1) is probably the
    best solution, not only because it's also the one with least typing. This
    will also help you not to cast away errors, which may happen if you have to
    change the type of the function's argument or the value passed to it latter.
    In your example, that might be not likely to happen, but with other types,
    like int's and long's it could cause problems.

    Except for their syntax (2) and (3) do basically the same, so if you really
    have to use a cast, use whichever looks better to you. There is less typing
    with a function-style cast (2), but it is easier to find a static_cast in a
    large program, especially with tools like grab. Also, a static_cast (or
    other *_cast's) can be used for all types whereas function-style casts can
    only be used for named types. For example, you can use static_cast<unsigned
    int>(...) but you cannot use unsigned int(...) as a cast.

    Regards
    Heinz
     
    Heinz Ozwirk, Aug 9, 2006
    #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. Kevin
    Replies:
    3
    Views:
    10,853
    AlecB
    Jul 7, 2006
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  3. howa
    Replies:
    2
    Views:
    102
    Richard Cornford
    Apr 15, 2007
  4. howa

    Perferred way to create object

    howa, Jul 4, 2007, in forum: Javascript
    Replies:
    2
    Views:
    88
  5. howa
    Replies:
    2
    Views:
    96
    scripts.contact
    Jul 26, 2007
Loading...

Share This Page