How can CString be passed to format string %s?

前端 未结 4 482
孤街浪徒
孤街浪徒 2020-12-02 01:25
class MyString
{
public:
    MyString(const std::wstring& s2)
    {
        s = s2;
    }

    operator LPCWSTR() const
    {
        return s.c_str();
    }
pri         


        
4条回答
  •  死守一世寂寞
    2020-12-02 02:14

    CString is specifically designed such that it only contains a pointer that points to the string data in a buffer class. When passed by value to printf it will be treated as a pointer when seeing the "%s" in the format string.

    It originally just happened to work with printf by chance, but this has later been kept as part of the class interface.


    This post is based on MS documentation long since retired, so I cannot link to their promise that they will continue to make this work.

    However, before adding more downvotes please also read this blog post from someone sharing my old knowledge:

    Big Brother helps you

提交回复
热议问题