(1)、指向函数的指针
指向函数的指针的一般定义形式是:
数据类型 (*函数指针名)(参数表)
例如:
指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int domat(int (*p)(int ,int ),int x,int y)
这里的*p是代表的函数指针 (int ,int )代表的是形参的类型
也就是说(*p)(int ,int ) 就是想到与调用一个函数;
```
(2)、 对象指针
声明对象指针的一般语法形式为:
类名 *对象指针名;
例如:
Point *pointPtr; //声明Point 类的对象指针变量 pointPtr
Point p1; //声明Point类的对象p1
pointPtr=&p1; //将对象p1的地址赋给PointPtr,使pointPtr指向p1
就像通过对象名来访问对象的成员一样,使用对象指针一样可以方便的访问对象的成员
语法形式为:
对象指针名->成员名
例如
``` bash
#include <iostream>
using namespace std;
class student
{
public:
int no;
};
void show1(student s){cout<<"no="<<s.no<<endl;}
void show2(student &s){cout<<"no="<<s.no<<endl;}
void show3(student *s){cout<<"no="<<s->no<<endl;}
void show4(student *s){cout<<"no="<<(*s).no<<endl;}
int main()
{
student s1;
s1.no=1;
show1(s1);
student &x=s1;
x.no=2;
show2(s1);
student *p=&s1;
p->no=3;
show3(&s1);
(*p).no=4;
show4(&s1);
}