#include <iostream>using namespacestd;classTest { Public: Test (): X (0), Y (0) {CNT++; } intx; inty; voidPrint ()Const; Static intCNT; Static voidprint_s ();//a static member function cannot be set to a const function (CV qualifier???)};inttest::cnt =0;//initialization of static member variablesvoidTest::p rint_s () {cout<<cnt<<"object (s) has (had) been created"<<", visited by print_s function"<<Endl;}voidTest::p rint ()Const{cout<<"x ="<<x<<"y ="<<y<<Endl;}intMain () {intTest::* ptr;//to declare a pointer to a class non-static member variable void(Test::* print_p) ()Const;//declaring a pointer to a class non-static member functionprint_p = &test::p rint;//Assign ValuePTR = &Test::x;//Assign ValueTest A; A.*ptr =1;//called(a.*print_p) ();//called, the preceding parentheses cannot be dropped (precedence of the operator) int*ptr_1 = &Test::cnt;//declaring and initializing pointers to class static member variablescout<<*ptr_1<<"object (s) has (had) been created"<<", visited by pointer"<<endl;//accessing static member variables through pointersTest::p rint_s ();//accessing static member variables through static member functions return 0;}
C + + pointer usage to class (non) static members