简体   繁体   English

如何检查MFC CString是否为空?

[英]How can I check if a MFC CString is null?

I want to check if a MFC CString is null or not. 我想检查MFC CString是否为null。 Is there a way to do this? 有没有办法做到这一点?

PS: I want to check if it is null not if it's empty. PS:我想检查它是否为null而不是它是否为空。

CString m_strName; CString m_strName;

在此处输入图片说明

A CString object is never NULL . CString对象从不为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. char*wchar* (可以为NULL ,作为指针的CString对象的内部缓冲区始终指向数据。 For a given CString object, you can only differentiate whether it is empty or not using CString::IsEmpty() . 对于给定的CString对象,只能使用CString::IsEmpty()区分它是否为空。

For the same reason, the LPCTSTR cast operator never returns NULL . 出于相同的原因, LPCTSTR运算符从不返回NULL

Due to the internal layout of the CString class template 1) , the pointer stored cannot ever be NULL . 由于CString类模板1)的内部布局,因此存储的指针永远不能为NULL

The CString class template has a single class member: m_pszData . CString类模板具有一个类成员: m_pszData This member not only contains the string data, but also additional information (like string length, reference count, buffer capacity, etc.; see CStringData ). 该成员不仅包含字符串数据,还包含其他信息(例如字符串长度,引用计数,缓冲区容量等;请参见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 . 由于字符串数据始终需要存在,因此m_pszData永远不能为NULL


1) CString is a typedef for a particular CStringT template instantiation. 1) CString是用于特定CStringT模板实例化的typedef。 The CStringT itself is derived from the CSimpleStringT class template. CStringT本身是从CSimpleStringT类模板派生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM