天天看点

面向对象程序设计 类 指向类的成员的指针

(文末附四种指针的使用方法对比)

1、指向类的非静态数据成员的指针

//指针定义形式
类型说明符 类名::*指针名;
指针名=&类名::数据成员名;

//指针使用形式
对象名.*指针名
           

这样赋值以后的指针是不能使用的,只有通过对象才能访问数据成员。

#include<iostream>
using namespace std;
class test
{
public:
	int x;
	test():x(0)
	{
	}
	test(int a):x(a)
	{
	}
	void disp()
	{
		cout<<"x="<<x<<endl;
	}
};
int main()
{
	test p(10);
	cout<<"p:";p.disp();
	int test::*q;
	q=&test::x;
	p.*q=20;
	cout<<"p:";p.disp();
}
/*result:
p:x=10
p:x=20
*/
           

main函数中

p.*q=20;

相当于令

p.x=20;

。需要注意的是,程序中数据成员为public域,否则编译会有错误,因为main函数不能直接访问private数据成员。

2、指向类的非静态成员函数的指针

//指针定义形式
类型说明符 (类名::*指针名)(参数表);
指针名=类名::成员函数名;

//指针使用形式
(对象名.*指针名)(参数表);
           

与指向非静态数据成员的指针一样,这样定义的指针是不能使用的,必须通过对象来访问成员函数。

#include<iostream>
using namespace std;
class test
{
	int x;
public:
	test():x(0)
	{
	}
	test(int a):x(a)
	{
	}
	void setx(int a)
	{
		x=a;
	}
	void disp()
	{
		cout<<"x="<<x<<endl;
	}
};
int main()
{
	test p(10);
	cout<<"p:";p.disp();
	void (test::*q)(int);
	q=&test::setx;//setx函数后面没有括号
	(p.*q)(20);//注意格式
	cout<<"p:";p.disp();
}
/*result:
p:x=10
p:x=20
*/
           

需要注意指向非静态成员函数指针使用的格式

void (test::*q)(int);q=&test::setx;

注意setx函数后面没有括号,当通过对象来访问成员函数的时候,也需要注意

(p.*q)(20);

格式。

3、指向类的静态数据成员的指针

//指针定义形式
类型说明符 *指针名;
指针名=&类名::静态数据成员名;

//指针使用形式
*指针名=x;
x=*指针名;//或者
           
#include<iostream>
using namespace std;
class test
{
	int x;
public:
	static int y;//静态数据成员的声明
	test():x(0)
	{
	}
	test(int a):x(a)
	{
	}
	void disp()
	{
		cout<<"x="<<x<<endl;
	}
};
int test::y=100;//静态数据成员的定义
int main()
{
	int *p;
	p=&test::y;
	cout<<"y="<<*p<<endl;
}
/*result:
y=100
*/
           

还是要注意private域和public域。

4、指向类的静态成员函数的指针

//指针定义形式
类型说明符 (*指针名)(参数表);
指针名=类名::静态成员函数;

//指针使用形式
指针名(实际参数表);
(*指针名)(实际参数表);//或者
           
#include<iostream>
using namespace std;
class test
{
public:
	int x;
	static int y;
	test(int a,int b):x(a)
	{
		y=b;
	}
	static void sety(int s)
	{
		y=s;
	}
	void disp()
	{
		cout<<"x="<<x<<" "<<"y="<<y<<endl;
	} 
};
int test::y;
int main()
{
	void (*p)(int);
	p=test::sety;
	test q(10,10);
	p(20);
	q.disp();
} 
/*result:
x=10 y=20
*/
           

静态成员函数只能对静态数据成员进行操作。

为了方便各种指针的使用方法,下面对四种指针的使用方法进行了汇总:

#include<iostream>
using namespace std;
class test
{
public://注意数据成员也为public域,否则无法访问,编译错误 
	int x;
	static int y;//静态数据成员 
public:
	test(int a,int b)
	{
		x=a;y=b;
	}
	void setx(int s)
	{
		x=s;
	}
	static void sety(int s)//静态成员函数 
	{
		y=s;
	}
	void disp()
	{
		cout<<"x="<<x<<" "<<"y="<<y<<endl;
	}
};
int test::y;//静态数据成员的定义 
int main()
{
	cout<<"//指向类的非静态数据成员的指针"<<endl;
	int test::*p;
	p=&test::x;
	test pt(10,20);
	pt.*p=20;
	pt.disp();
	cout<<"//指向类的非静态成员函数的指针"<<endl;
	void (test::*q)(int);
	q=test::setx;
	test qt(30,40);
	(qt.*q)(40);
	qt.disp();
	cout<<"//指向类的静态数据成员的指针"<<endl;
	int *r;
	r=&test::y;
	test rt(50,60);
	cout<<"y="<<*r<<endl;
	cout<<"//指向类的静态成员函数的指针"<<endl;
	void (*s)(int);
	s=test::sety;
	test st(70,80);
	s(70);
	st.disp();
}
/*result:
//指向类的非静态数据成员的指针
x=20 y=20 
//指向类的非静态成员函数的指针
x=40 y=40 
//指向类的静态数据成员的指针
y=60 
//指向类的静态成员函数的指针 
x=70 y=70
*/
           

继续阅读