A reference can not be NULL or it can be NULL?

后端 未结 7 932
忘了有多久
忘了有多久 2020-12-13 16:03

I have read from the Wikipedia that:

“References cannot be null, whereas pointers can; every reference refers to some object, although it may or may n

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 16:41

    In your code:

    person *object=NULL;
    person &object1=*object;
    

    you dereference a NULL pointer, so you get undefined behaviour. And to answer your question, there is no such thing as a NULL reference.

    And to address the other part of your question, just because a program compiles, there is no guarantee that it is correct or that it will work. C++ compilers are not required to even attempt to diagnose the kind of error your code contains.

提交回复
热议问题