我已經從維基百科上寫着:引用不能爲NULL或可以爲NULL?
「References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.」
但我不因爲下面的代碼看看它的編譯器沒有給出錯誤認爲:
class person
{
public:
virtual void setage()=0;
};
main()
{
person *object=NULL;
person &object1=*object;
}
請詳細說明了這一點。
文章或許應該說:「在一個結構良好的程序中的每個參考引用了一個對象」解引用空指針當然是不合格的。 – GManNickG 2010-01-29 21:08:45
我想你的意思是'person&object1 = object;',這確實會給你一個編譯器錯誤(不匹配的類型)。 – 2010-01-29 21:10:26
讓我們不是都忘記我們最喜歡的空引用的故事:http://www.gotw.ca/conv/002.htm – GManNickG 2010-01-29 21:12:14