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;
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;
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
.
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.
© 2022 - 2024 — McMap. All rights reserved.
if (mypointer == NULL) ...
, but that is rather obvious. – Gretnatokres
is not properly initialized. That's why all class members are pretty much garbage. You should post entire code. So we can help you out. – Sapptokres.m_strName
is aCString
object, and not a pointer to aCString
object (i.e. aCString*
)? The Watch window seems to imply, that it is a pointer. – Donleytokres
? – Gretnatokres
is. Post the real code. Everything else is just asking us to guess, and my guess is, that you are doing it wrong. ACString
class member will always be initialized, regardless of how the object is constructed. ACString*
as a class member may or may not be initialized. You also need to answer what debug visualizers you have installed. – Donley