引用不能为NULL或可以为NULL? [英] A reference can not be NULL or it can be NULL?

查看:729
本文介绍了引用不能为NULL或可以为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已从维基百科中读取:

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 not be valid."

但是我不相信因为下面的代码看看它编译器没有错误:

But I don’t believe because of following code look at it compiler gives no error:

class person
{
public:
virtual void setage()=0;
};
main()
{
person *object=NULL;
person &object1=*object;
}

请详细说明这一点。

推荐答案

person& object1 = * object 与说 person& ; object1 = NULL 。可能编译器只是不够聪明,发现你是解引用空指针,但你会得到一个运行时错误。所以他们是一种真实的;)

Saying person &object1=*object is not the same thing as saying person &object1=NULL. Probably the compiler is just not smart enough to find out that you are dereferencing null pointer, but you'll get a runtime error anyway. So they are kind of true still ;)

这篇关于引用不能为NULL或可以为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆