Pointer vs. Reference

后端 未结 12 1712
闹比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:43

    My rule of thumb is:

    Use pointers if you want to do pointer arithmetic with them (e.g. incrementing the pointer address to step through an array) or if you ever have to pass a NULL-pointer.

    Use references otherwise.

提交回复
热议问题