Meaning of pass by reference in C and C++?

后端 未结 10 1104
生来不讨喜
生来不讨喜 2020-12-17 20:46

I am confused about the meaning of \"pass by reference\" in C and C++.

In C, there are no references. So I guess pass by reference means passing a pointer. But then

10条回答
  •  情歌与酒
    2020-12-17 21:41

    In C, there are no references

    There are no reference variables. But you can refer to objects using pointers. Therefore pointers are "references" from an abstract point of view.

    But then why not call it pass by pointer?

    You can call it pass by pointer. Reference is a more general term than pointer. It is often preferable to use the more general term when you want to discuss abstractions and want to ignore implementation details. You would call it pass by reference for the same reason that you call a variable "integer" rather than "int32_t".

    In C++, we have both pointers and references (and stuff like iterators that lies close). So what does pass by reference mean here?

    Depends on context. Often it means that the function argument is a reference variable, but it may also refer to a pointer, iterator, a reference wrapper... anything that referes to an object.


    Reference is an abstract concept that exists beyond c and c++; even beyond programming. In c++, the term is ambiguous with reference variables and the context and convention (which isn't universal) determines the meaning.

提交回复
热议问题