C++地址运算符的用途?
可能的重复:
为什么使用指针?
我知道 C++ 和 C++ 是什么。做。但它能用来做什么呢?
Possible Duplicate:
Why use pointers?
I know what the C++ & does. but what can it be used for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
&
用于在调用站点使用时将参数(指针)的地址传递给函数。&
用于函数参数列表中,通过引用传递参数。&
是按位AND
。例如(a & b)
&
用于逻辑AND
。在本例中,两个&
构成逻辑AND
。例如(a && b)
。&
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 bitwiseAND
. e.g.(a & b)
&
is used in logicalAND
. In this case, two&
make logicalAND
. e.g(a && b)
.例如,将指向对象的指针传递给某个函数。
For example to pass a pointer to your object into some function.
STL 或其他常用库中的许多函数需要指向对象(而不是对象本身)的指针。另外,很多时候您会想要传递指针。当您需要时,
&
运算符允许您获取指向您有权访问的任何对象的指针。浏览
boost
库并找到一些。一个示例:要传递指向
Y
的指针,您必须使用&
运算符。此外,您的个人资料表明您喜欢 3D 游戏。我所知道的几乎每个 C++ 3d 库都使用指向浮点数或整数数组的指针来操作所有内容。您需要
&
运算符将指针传递给这些数组。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: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.