Reputation: 389
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;
Upvotes: 5
Views: 16046
Reputation: 51464
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
.
CString
is a typedef for a particular CStringT
template instantiation. The CStringT
itself is derived from the CSimpleStringT
class template.
Upvotes: 2
Reputation: 6556
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
.
Upvotes: 11