C++ Address-of 运算符:&

来自泡泡学习笔记
BrainBs讨论 | 贡献2024年10月10日 (四) 15:33的版本 (创建页面,内容为“==示例:静态成员的地址== // 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>…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

示例:静态成员的地址

// 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