c++中的引用可不可以指向一个null

ksword 2003-03-12 10:41:38
比如fact& fact1=fact.getSmmByID(id)
但是这个函数的返回值可能是一个null
...全文
354 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigdoors 2003-03-12
  • 打赏
  • 举报
回复

引用是别名的意思,使引用就要知道对象本身在哪里,
引用指向一个null指针会导致C++标准中的未定义行为,
绝对不要这样做
bugfree 2003-03-12
  • 打赏
  • 举报
回复
no, you can not. look at the following error message in gcc

ref.c: In function `int main()':
ref.c:6: initialization of non-const reference type `int&' from rvalue of type
`int'
ref.c:6: warning: initialization to non-pointer type `int' from NULL
ref.c:6: warning: argument to non-pointer type `int' from NULL
[gdc5e46:(allanz)]>cat -n ref.c
1 #include <iostream>
2 int main()
3 {
4 int j=8;
5 //int &i=j;
6 int &i=NULL;
7 }
winco 2003-03-12
  • 打赏
  • 举报
回复
那就是NULL吧。
这种情况也就相当于给fact1赋null值

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧