Pointer vs. Reference

后端 未结 12 1636
闹比i
闹比i 2020-11-22 11:13

What would be better practice when giving a function the original variable to work with:

unsigned long x = 4;

void func1(unsigned long& val) {
     val          


        
12条回答
  •  爱一瞬间的悲伤
    2020-11-22 11:44

    A reference is similar to a pointer, except that you don’t need to use a prefix ∗ to access the value referred to by the reference. Also, a reference cannot be made to refer to a different object after its initialization.

    References are particularly useful for specifying function arguments.

    for more information see "A Tour of C++" by "Bjarne Stroustrup" (2014) Pages 11-12

提交回复
热议问题