/* this 指针 */ #include <iostream> using namespace std; class Point { private: double x; double y; static int count; public: Point(double a=0, double b=0) : x(a), y(b) { count++; } void Show()const; void setPoint(double x, double y); static int getCount(); }; int Point::count=0; void Point::Show()const { cout<<"this: "<<this<<endl; cout<<"x="<<x<<"y="<<y<<endl; } int Point::getCount() { //return this->count; //错,在静态成员函数内部没有this指针 return count; } void Point::setPoint(double x, double y) { this->x=x; this->y=y; } int main() { Point p1(1,2); cout<<"p1: "<<&p1<<endl; p1.Show(); cout<<endl; Point p2; p2.setPoint(5,6); cout<<"p2: "<<&p2<<endl; p2.Show(); return 0; }