Skip to content

Commit

Permalink
2021-05-04 22:33:33 CST W18D2
Browse files Browse the repository at this point in the history
  • Loading branch information
aggresss committed May 4, 2021
1 parent bdbf460 commit a90d3d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions workbench/pointer_to_members/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 类成员指针


成员函数指针的调用必须通过类对象,和操作符 `.*``->*`

## Reference

- https://www.ibm.com/docs/en/zos/2.2.0?topic=only-pointers-members-c
-
30 changes: 30 additions & 0 deletions workbench/pointer_to_members/hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
using namespace std;

class X {
public:
int a;
void f(int b) {
cout << "The value of b is " << b << endl;
}
};

int main() {
// declare pointer to data member
//int X::*ptiptr = nullptr;
int X::*ptiptr = &X::a;

// declare a pointer to member function
void (X::*ptfptr)(int) = &X::f;

// create an object of class type X
X xobject;

// initialize data member
xobject.*ptiptr = 10;

cout << "The value of a is " << xobject.*ptiptr << endl;

// call member function
(xobject.*ptfptr)(20);
}

0 comments on commit a90d3d3

Please sign in to comment.