此代码似乎实现在C ++中返回null引用 [英] This code appears to achieve the return of a null reference in C++
问题描述
我的C ++知识有点零散。我在工作中重做一些代码。我改变了一个函数来返回一个类型的引用。在内部,我基于传入的标识符查找对象,然后返回对该对象的引用(如果找到)。当然,我遇到了如果我没有找到对象,返回的问题,在环顾四周,许多人声称在C ++中返回一个null引用是不可能的。基于这个建议,我尝试了返回一个成功/失败布尔值,并使对象引用一个out参数的把戏。但是,我遇到了需要初始化我将作为实际参数传递的引用的路障,当然没有办法这样做。我退回到通常的方法只是返回一个指针。
My C++ knowledge is somewhat piecemeal. I was reworking some code at work. I changed a function to return a reference to a type. Inside, I look up an object based on an identifier passed in, then return a reference to the object if found. Of course I ran into the issue of what to return if I don't find the object, and in looking around the web, many people claim that returning a "null reference" in C++ is impossible. Based on this advice, I tried the trick of returning a success/fail boolean, and making the object reference an out parameter. However, I ran into the roadblock of needing to initialize the references I would pass as actual parameters, and of course there is no way to do this. I retreated to the usual approach of just returning a pointer.
我问一个同事。他经常使用以下技巧,这是最近版本的Sun编译器和gcc接受的:
I asked a colleague about it. He uses the following trick quite often, which is accepted by both a recent version of the Sun compiler and by gcc:
MyType& someFunc(int id)
{
// successful case here:
// ...
// fail case:
return *static_cast<MyType*>(0);
}
// Use:
...
MyType& mt = somefunc(myIdNum);
if (&mt) // test for "null reference"
{
// whatever
}
...
我一直在维护这个代码库,但我发现我没有足够的时间看关于语言的小细节,我想。我一直在浏览我的参考书,但对此的答案逃避了我。
I have been maintaining this code base for a while, but I find that I don't have as much time to look up the small details about the language as I would like. I've been digging through my reference book but the answer to this one eludes me.
现在,我几年前有一个C ++课程,在那里我们强调在C ++中,一切都是类型,所以我在尝试保持这一点。解构表达式:* static_cast< MyType *>(0);,在我看来,我们取一个字面值零,将它转换为一个指向MyType的指针(使它成为一个空指针),然后应用在分配给引用类型(返回类型)的上下文中,它应该给我一个对指针指向的同一对象的引用。
Now, I had a C++ course a few years ago, and therein we emphasized that in C++ everything is types, so I try to keep that in mind when thinking things through. Deconstructing the expression: "*static_cast<MyType*>(0);", it indeed seems to me that we take a literal zero, cast it to a pointer to MyType (which makes it a null pointer), and then apply the dereferencing operator in the context of assigning to a reference type (the return type), which should give me a reference to the same object pointed to by the pointer. This sure looks like returning a null reference to me.
任何建议解释为什么这是有效的(或为什么它不应该)。
Any advice in explaining why this works (or why it shouldn't) would be greatly appreciated.
谢谢,
Chuck
Thanks, Chuck
推荐答案
虽然它可能似乎工作。此行取消引用一个空指针:
This code doesn't work, though it may appear to work. This line dereferences a null pointer:
return *static_cast<MyType*>(0);
零,转换为指针类型,然后使用一元 - *
解除引用此空指针。
The zero, cast to a pointer type, results in a null pointer; this null pointer is then dereferenced using the unary-*
.
引用空指针导致未定义的行为,因此你的程序可以做任何事情。在你描述的例子中,你得到一个空引用(或者,它看起来你得到一个空引用),但它也是合理的程序崩溃或其他任何发生。
Dereferencing a null pointer results in undefined behavior, so your program may do anything. In the example you describe, you get a "null reference" (or, it appears you get a null reference), but it would also be reasonable for your program to crash or for anything else to happen.
这篇关于此代码似乎实现在C ++中返回null引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!