//----->fun2 start//使用静态变量class test { static int N; static int sum;public : test() { sum += ++N; } static void reset() { N = sum = 0; } static int getSum() { return sum; }};int test::N = 0;int test::sum = 0;void function2() { test::reset(); test *p = new test[10]; cout << test::getSum() << endl; delete[]p;}//--->fun2 end
使用虚函数的编译多态性
123456789101112131415161718192021222324
//----->fun3 start//使用虚函数的编译多态性class A {public: virtual int sum(int n) { return 0; };};class B : public A {public: int sum(int n) { A a; B b; A *p[2] = {&a, &b}; return n + p[(n - 1 != 0)]->sum(n - 1); }};void function3() { B b; cout << b.sum(10) << endl;}//----->fun3 end