C++的指针
- C++可以定义-成员指针变量-(指向成员的指针,而不是指针型成员)
- 成员指针变量保存的是成员在结构体中的相对地址
- 成员指针的定义:
- int Student::*mp = &Student::age;
- 成员指针的调用:
- st.*mp = 18;
- 示例:
```cpp
include
include
using namespace std;
struct Student{ int number; bool gender; int age; };
int main(int argc, char const *argv[]) { struct Student st = {1907201, 1, 18}; cout << st.number << endl;
int *p1 = &st.number;
bool *p2 = &st.gender;
int *p3 = &st.age;
cout << p1 << endl;
cout << p2 << endl;
cout << p3 << endl;
cout << "age : " << *p3 << endl;
cout << "成员指针变量" << endl;
int Student::*mp1 = &Student::number;
bool Student::*mp2 = &Student::gender;
int Student::*mp3 = &Student::age;
printf("%p\n", mp1);
printf("%p\n", mp2);
printf("%p\n", mp3);
cout << "age : " << st.*mp3 << endl;
return 0;
} ```