""空字符串,會創建一個對象,分配內存空間
NULL,不會創建對象,不會分配內存空間,意思是不存在的
#include <iostream>
using namespace std;
int main()
{
std::string strTemp;
if( strTemp.empty() )
{
cout<<"is empty "<<endl;
}
else
{
cout<<"is not empty"<<endl;
}
strTemp = "";
if( strTemp.empty() )
{
cout<<"is empty"<<endl;
}
else
{
cout<<"is not empty"<<endl;
}
if( strTemp == "" )
{
cout<<"is empty"<<endl;
}
else
{
cout<<"is not empty"<<endl;
}
/*
if( strTemp == NULL )
{
cout<<"is empty"<<endl;
}
else
{
cout<<"is not empty"<<endl;
}
*/
return 0;
}在以下代碼最后,用NULL與字符類型進行比較,會直接導致編譯錯誤!
test.cpp:37:17: error: no match for ‘operator==’ in ‘strTemp == 0’
從編譯的錯誤提示看:NULL為數值0
#include <iostream>
using namespace std;
int main()
{
if( NULL == 0 )
{
cout<<"NULL is number 0"<<endl;
}
else
{
cout<<"NULL is not number 0"<<endl;
}
int nNum;
cout<<"nNUm = "<<nNum<<endl;
nNum = NULL;
cout<<"nNUm = "<<nNum<<endl;
cout<<static_cast<void*>(NULL)<<endl;
return 0;
}從這段代碼,進一步驗證了,NULL對就的值為數值0,地址為空
注意:將NULL賦值為int變量時會報警告
NULL.cpp:18:9: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。