Why is a c++ reference considered safer than a pointer?

后端 未结 9 1576
我寻月下人不归
我寻月下人不归 2020-12-03 18:23

When the c++ compiler generates very similar assembler code for a reference and pointer, why is using references preferred (and considered safer) compared to pointers?

9条回答
  •  鱼传尺愫
    2020-12-03 18:41

    It is a little safer, but not the same thing. Note that you have the same problems of "dangling references" as with "dangling pointers". For instance, returning a reference from a scoped object yields undefined behaviour, exactly the same as pointers:

    int& f() { int x = 2; return x; }
    

    The only benefit is that you cannot create a null reference. Even if you try hard:

    int& null_ref = *((int*)0); // Dereferencing a null pointer is undefined in C++
                                // The variable null_ref has an undefined state.
    

    As class members, pointers are preferred since they have better assignment semantics: you cannot reassign a reference when it has been initialized. The compiler won't be able to provide a default assignment operator if there are reference members in the class.

    Therefore, C++ cannot get rid of pointers, and you can use them freely: by passing arguments as pointers and not as (non const) references, you make it clear at the call site that the object will be modified. This can add a little safety, since you see by naked eye what functionsindeed modify objects.

    I play a little the devil's advocate, but references are easy to abuse.

提交回复
热议问题