Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a MFC CString is null?

I want to check if a MFC CString is null or not. Is there a way to do this?

PS: I want to check if it is null not if it's empty.

CString m_strName;

enter image description here

like image 568
Mihai Avatar asked Nov 24 '15 09:11

Mihai


2 Answers

A CString object is never NULL. Unlike a char* or wchar*, which can be NULL, the internal buffer of a CString object which is a pointer always points to a data. For a given CString object, you can only differentiate whether it is empty or not using CString::IsEmpty().

For the same reason, the LPCTSTR cast operator never returns NULL.

like image 166
Andrew Komiagin Avatar answered Oct 08 '22 01:10

Andrew Komiagin


Due to the internal layout of the CString class template1), the pointer stored cannot ever be NULL.

The CString class template has a single class member: m_pszData. This member not only contains the string data, but also additional information (like string length, reference count, buffer capacity, etc.; see CStringData). This additional information is stored to the left of the stored pointer. Both parts (string data and character buffer) must be allocated in a single block of memory, as there is only one pointer to reference both. Since the string data always needs to be there, the m_pszData can never be NULL.


1)CString is a typedef for a particular CStringT template instantiation. The CStringT itself is derived from the CSimpleStringT class template.
like image 1
IInspectable Avatar answered Oct 08 '22 01:10

IInspectable