C++ address operator uses? [duplicate]

試著忘記壹切 提交于 2020-01-22 02:08:05

问题


Possible Duplicate:
Why use pointers?

I know what the C++ & does. but what can it be used for?


回答1:


  • & is used to pass address of arguments (pointer) to function, when it's used at calling site.
  • & is used to pass arguments by reference to function, when it's used in function parameter list.
  • & is bitwise AND. e.g. (a & b)
  • & is used in logical AND. In this case, two & make logical AND. e.g (a && b).



回答2:


For example to pass a pointer to your object into some function.




回答3:


Many functions in the STL or other commonly available libraries requires a pointer to an object (not the object itself). Also, many time you'll want to pass pointers. When you need that, the & operator allows you to get a pointer to any object you have access to.

Browse through the boost libraries and find some. One example:

template<class Y> explicit shared_ptr(Y * p);

To pass in a pointer to a Y you'd have to use the & operator.

Furthermore, your profile says you're into 3-d games. Almost every C++ 3d library I know of uses pointers to arrays of floats or ints to manipulate everything. You need the & operator to pass in the pointers to those arrays.



来源:https://stackoverflow.com/questions/5051894/c-address-operator-uses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!