Why are there many types of strings in MFC?

LPTSTR* arrpsz = new LPTSTR[ m_iNumColumns ];
arrpsz[ 0 ] = new TCHAR[ lstrlen( pszText ) + 1 ];
(void)lstrcpy( arrpsz[ 0 ], pszText ); 

      

This is a code snippet about String in MFC , and there is also _T ("HELLO"). Why are there so many string types in MFC? What are they used for?

+2


source to share


3 answers


Strictly speaking, what you are showing here are Windows specific strings, not MFC String types (but your point is even better if you add to CString and std :: string). This is more difficult than it needs to be - mostly for historical reasons.

tchar.h is definitely worth a look - also look for TCHAR on MSDN.



There is an old joke about string processing in C that may seem funny: string processing in C is so efficient that there is no string type.

+2


source


Historical reasons.

The original windows APIs were in C (unless the real originals were in Pascal and got lost in the fog). Microsoft has created its own data types to represent C data types, probably because C data types are not standard in size. (For C-integral types, char

is at least 8 bits, short

is at least 16 bits and is not less than char

, int

is at least 16 bits and is not less than short

and long

is at least 32 bits and is at least as large as int

.) Since Windows ran essentially 16-bit systems first and later 32-bit systems, C compilers did not necessarily agree on sizes. Microsoft further denoted more complex types, so (if I got that right) C char *

would be calledLPCSTR

...

Thing is, an 8-bit character is not suitable for Unicode as UTF-8 is not easy to modify in C or C ++. Therefore, they need a wide character type that C will call it wchar_t

, but which has received a set of Microsoft data types corresponding to earlier ones. Also, since people can sometimes compile to Unicode and sometimes ASCII, they create a TCHAR character type and corresponding string types that will be based on either char

(for ASCII compilation) or wchar_t

(for Unicode).



Then came MFC and C ++ (sigh of relief), and Microsoft came with the string type. Since it was before the C ++ standardization, there wasn't std::string

, so they invented CString

. (They also had container classes that were not compatible with what became the STL and then the containers in the library.)

Like any mature and heavily used application or API, there are many things that would be done completely differently if you could do it from scratch.

+2


source


See Mapping common text in TCHAR.H and description LPTSTR

in Windows Data Types .

0


source







All Articles