CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    25

    [RESOLVED] is const char* to string conversion safe?

    we have a function f with gets a string as below
    Code:
    void f( std::string str )
    {
    // body
    }
    is it safe to call this function with a const char* varialbe like below
    Code:
    void g( const char* phrase )
    {
    f(phrase);
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: is const char* to string conversion safe?

    Yes.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: [RESOLVED] is const char* to string conversion safe?

    The argument is non-reference, non-pointer std::string, the object of type string will always be created even if you pass string object. Appropriate constructor would be called for initializing the function argument.
    When function returns, the destructor will be called.

    Thus, is it absolutely safe.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured