C++ 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; }
&d equals &rd
示例:函数地址作为参数
// 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 }
25