//本程序用来区别下面六种指针:1.普通指针 2.普通函数指针 3.指向类的数据成员的指针
//4.指向类的成员函数的指针 5.指向类的静态数据成员的指针 6.指向类的静态成员函数的指针
#include<iostream>
#include<string>
using namespace std;
class Myclass
{
public:
Myclass(int i=0,int j=0 ):_weight(i),_height(j){}
void print()
{
cout<<_weight<<","<<_height<<","<<the_same<<endl;
}
static void modifythesame(int xx)
{
the_same=xx;
}//静态成员函数只能修改静态数据成员
public:
int _weight,_height;
static the_same;
};
int Myclass::the_same=0;
int max(int a,int b)
{return a<b? b:a;
}
int a=2,b=3;
Myclass myclass;
int *p1=&a; //第一种:普通指针
int (*p2)(int ,int )=max; //第二种:普通函数指针
int Myclass::*p3=&Myclass::_height ;//第三种指针:指向类的数据成员的指针,与普通
//的指针的区别在于左右两边要加上:类名::
void (Myclass::*p4)()=&Myclass::print;//第四种指针:指向类的成员函数的指针,与普通
//的函数指针的区别也是在左右两边加上:类名::
int *p5=&Myclass::the_same;//第五种指针:指向类的静态数据成员的指针,左边是普通指针
//一样,右边要加上:类名::
void (*p6)(int)=&Myclass::modifythesame;// 第六种指针:指向类的静态成员函数的指针
//左边与普通函数指针一样,右边要加上:类名::
void main()
{
cout<<*p1<<endl;//第一种普通指针的使用
cout<<p2(a,b)<<endl;//第二种普通函数指针的使用
cout<<myclass.*p3<<endl;//第三种指向类的数据成员的指针的使用,将原来的数据成员名用
//*P代替即可
(myclass.*p4)();//第四种指向类的成员函数的指针的使用,将原来的成员函数名用*p代替,并且
//要用()括起来,这个地方容易忘记
cout<<*p5<<endl;//第五种指向类的静态数据成员的指针的使用,同普通指针的使用
p6(a); //第六种指向类的静态成员函数的指针的使用,同普通函数指针的使用
cout<<*p5<<endl;
}
/*犯的错误是:
(1)第四种指针的使用的时候没有加();
(2)当时将_weight定义为private,导致了cout<<myclass.*p3<<endl;没有办法访问私有成员
*/