LPCTSTR to CString. Cast from pointer type const CHAR* is not allowed

재훈 정 21 Reputation points
2021-08-25T08:32:56.36+00:00

JOINT STRIKE FLGHTER, AIR VEHICLE, C++ CODING STANDARDS Chapter 4.23 Type Conversions, AV Rule 182.

How can the conversion be successful?

Developer technologies C++
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2021-08-25T08:47:59.297+00:00

    Pass the const char* pointer to the narrow string to the CString class constructor.
    For example,

    LPCTSTR psz = "narrow string";
    CString str(psz); // Construct CString object
    printf_s("Null terminated string : %s\n", psz);
    printf_s(("CString object : %s\n"), str.GetString());
    

    Another way to construct the CString object-

    CString str = psz; // Construct CString object
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. 재훈 정 21 Reputation points
    2021-08-25T23:32:33.527+00:00

    @RLWA32

    https://www.parasoft.com/

    This is the result of checking the code using the c/c++ test tool from parasoft, a reliability test tool company.

    The project is set to multibyte.
    void foo2(CString strPath)
    {
    //strPath
    }

    void foo(LPCTSTR strPath)
    {
    foo2(CString(strPath)); << this line
    }


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.