且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么指针不能转换为引用?

更新时间:2023-11-11 23:01:16

引用可以实现引擎盖下的指针条件是无关紧要的。许多编程概念可以在其他方面实现。你可以问为什么我们有,而$ c>循环可以用 goto jmp 。不同语言概念的意义在于使得程序员更容易,而引用是为程序员方便而设计的语言概念。



您可能误解了参考文献的目的。引用给你指针的积极一面(廉价传递),但是由于它们具有与常规值相同的语义,它们消除了使用指针所带来的很多危险:(指针运算,悬挂指针等)更多重要的是,引用是一个完全不同的类型比C ++类型系统中的指针,并且疯狂允许两个是可互换的(这将失去引用的目的) p>

参考语法是为了反映常规值语义的语法而设计的,同时为您提供了廉价传递内存的能力



现在,转到您的示例:

  FooRef(* p); //为什么我必须取消引用指针? 

您必须在这里取消引用指针,因为 FooRef int 采用引用,而不是对 int * 的引用。注意,你也可以有一个指针的引用:

  void FooPointerRef(const int *& ;); 

引用指针的函数可以从内部修改指针的内存地址功能。在您的示例中,您必须显式地将指针解引用到镜像值语义。否则,看着函数调用 FooRef(p)的人会认为 FooRef - 值或引用指针 - 但不是(非指针)值或引用。


I've read in multiple sources that a C++ reference is no more than a pointer with compile time restrictions.

If this is true, how come I am forced to dereference a pointer in order to pass it to a function that expects a parameter?

void FooRef(const int&);
void FooPointer(const int*);

int main()
{
    int* p = new int(5);
    FooPointer(p);
    FooRef(*p); // why do I have to dereference the pointer?

    ...
}

As I understand it, if I were to pass an int to FooRef the compiler would create the pointer (reference) from the address of the variable for me, but if the type is already a pointer then dereferencing it seems pointless. It seems to me like I am dereferencing a pointer, just to let the compiler create another pointer from the dereferenced value which seems senseless to me.
Wouldn't it be simpler / more performant to just copy the pointer instead of just referencing+derferencing the value? (Perhaps this is really what's happening?)

Am I missing something here? and does calling FooRef in such a scenario be slower than calling FooPointer?
And do references and pointers really produce the same code during compilation?

The fact that references can be implemented in terms of pointers under the hood is irrelevant. Many programming concepts can be implemented in terms of other things. You may as well ask why we have while loops when while can be implemented in terms of goto or jmp. The point of different language concepts is to make things easier for the programmer, and references are a language-concept designed for the convenience of the programmer.

You probably are misunderstanding the purpose of references. References give you the positive side of pointers (cheap to pass around), but since they have the same semantics as regular values, they remove a lot of the dangers that come with using pointers: (pointer arithmetic, dangling pointers, etc.) More importantly, a reference is a totally different type than a pointer in the C++ type-system, and it would be madness to allow the two to be interchangeable (that would defeat the purpose of references.)

Reference syntax is designed on purpose to mirror the syntax of regular value semantics - while at the same time providing you with the ability to cheaply pass around memory addresses instead of copying entire values.

Now, turning to your example:

FooRef(*p); // why do I have to dereference the pointer?

You have to dereference the pointer here because FooRef takes a reference to an int, not a reference to an int*. Note that you can also have a reference to a pointer:

void FooPointerRef(const int*&);

A function that takes a reference to a pointer enables you to modify the memory address of the pointer from within the function. In your example, you have to explicitly dereference the pointer to mirror value semantics. Otherwise, someone looking at the function call FooRef(p) is going to think that FooRef either takes a pointer-by-value or a pointer-by-reference - but NOT a (non-pointer) value or a reference.