C++_讲解C++编程中Address-of运算符 cast-expression备
在将 address-of 运算符应用于限定名时,结果将取决于 qualified-name 是否指定静态成员。如果是这样,则结果为指向成员声明中指定的类型的指针。如果该成员不是静态的,则结果为指向由 qualified-class-name 指示的类的成员 name 的指针。
Output
以下示例使用 address-of 运算符将指针参数传递给函数:
// expre_Address_Of_Operator.cpp // C2440 expected class PTM { public: int iValue; static float fValue; }; int main() { int PTM::*piValue = &PTM::iValue; // OK: non-static float PTM::*pfValue = &PTM::fValue; // C2440 error: static float *spfValue = &PTM::fValue; // OK }
// expre_Address_Of_Operator2.cpp // compile with: /EHsc #include <iostream> using namespace std; int main() { double d; // Define an object of type double. double& rd = d; // Define a reference to the object. // Obtain and compare their addresses if( &d == &rd ) cout << "&d equals &rd" << endl; }
通过将 address-of 运算符应用于引用类型,可获得与将该运算符应用于引用绑定到的对象所获得的结果相同的结果。例如:以下代码段说明了结果的不同之处,取决于该成员是否为静态的:
address-of 运算符仅适用于具有基本、结构、类或在文件范围级别声明的联合类型的变量,或仅适用于下标数组引用。在这些表达式中,可在 address-of 表达式中添加或提取不包括 address-of 运算符的常数表达式。
// expre_Address_Of_Operator3.cpp // compile with: /EHsc // Demonstrate address-of operator & #include <iostream> using namespace std; // Function argument is pointer to type int int square( int *n ) { return (*n) * (*n); } int main() { int mynum = 5; cout << square( &mynum ) << endl; // pass address of int }
当应用于函数或左值时,该表达式的结果将是派生自操作数类型(右值)的指针类型。例如,如果操作数的类型为 char,则表达式的结果为指向 char 的类型指针。address-of 运算符(应用于 const 或 volatile 对象)的计算结果为 const type * 或 volatile type *,其中 type 是原始对象的类型。在此示例中,由于 fValue 是静态成员,因此表达式 &PTM::fValue 产生类型 float * 而不是类型 float PTM::*。
备注
仅当明确要引用的函数的版本时,才能采用重载函数的地址。有关如何获取特定重载函数的地址的信息,请参阅重载函数的地址。
&d equals &rd
相关热词: C++
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/cjj/6462.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
只需要在调用Ctrl+B编译后
时间:2021-01-13
-
OpenGL超级宝典visual studio
时间:2021-01-04
-
Directx11 教程(2) 基本的wi
时间:2021-01-04
-
LeetCode11ContainerWithMostWate
时间:2021-01-04
-
C语言简单IT之家速成
时间:2020-12-27
-
三分钟了解Activity工作流
时间:2020-12-27
-
编译器是如何实现32位整型
时间:2020-12-27
-
C++中lower_bound函数和upper
时间:2020-12-27
热门文章
-
LeetCode11ContainerWithMostWater(最大水容器)
时间:2021-01-04
-
C语言简单编程速成
时间:2020-12-23
-
都2020了,这五个最佳C++的IDE你还没用过?
时间:2020-12-23
-
C语言源程序文件的后缀是什么?
时间:2020-12-23
-
OpenGL超级宝典visual studio 2013开发环境配置
时间:2021-01-04
-
编译器是如何实现32位整型的常量整数除
时间:2020-12-27
-
libusbwin32学习笔记(二)
时间:2020-12-27
-
C语言简单IT之家速成
时间:2020-12-27
-
C语言和Python语言有什么区别呢?
时间:2020-12-24
-
C++对象模型之RTTI的实现原理
时间:2020-12-23
