Why was wchar_t invented?

前端 未结 10 1733
说谎
说谎 2021-01-03 20:15

Why is wchar_t needed? How is it superior to short (or __int16 or whatever)?

(If it matters: I live in Windows world. I don\'t

10条回答
  •  忘掉有多难
    2021-01-03 21:07

    Why is wchar_t needed? How is it superior to short (or __int16 or whatever)?

    In the C++ world, wchar_t is its own type (I think it's a typedef in C), so you can overload functions based on this. For example, this makes it possible to output wide characters and not to output their numerical value. In VC6, where wchar_t was just a typedef for unsigned short, this code

    wchar_t wch = L'A'
    std::wcout << wch;
    

    would output 65 because

    std::ostream::operator<<(unsigned short)
    

    was invoked. In newer VC versions wchar_t is a distinct type, so

    std::ostream::operator<<(wchar_t)
    

    is called, and that outputs A.

提交回复
热议问题