首页 > 编程语言 > 详细

C++静态成员变量和静态成员函数指针

时间:2020-03-14 18:39:08      阅读:103      评论:0      收藏:0      [点我收藏+]
#include <iostream>
using namespace std;

class Point {
public:
    Point(int x = 0, int y = 0) : x(x), y(y) {
        count++;
    }    
    Point(const Point &p) : x(p.x), y(p.y) {
        count++;
    }
    ~Point() {  count--; }
    int getX() const { return x; }
    int getY() const { return y; }
    static int count;

private:
    int x, y;
};

int Point::count = 0;

int main() {
    int *ptr = &Point::count;
    Point a(4, 5);
    cout << "Point A: " << a.getX() << ", " << a.getY();
    cout << " Object count = " << *ptr << endl;

    Point b(a);
    cout << "Point B: " << b.getX() << ", " << b.getY();
    cout << " Object count = " << *ptr << endl;

    return 0;
}

 

#include <iostream>
using namespace std;

class Point {
public:    
    Point(int x = 0, int y = 0) : x(x), y(y) {
        count++;
    }    
    Point(const Point &p) : x(p.x), y(p.y) {
        count++;
    }
    ~Point() {  count--; }
    int getX() const { return x; }
    int getY() const { return y; }

    static void showCount() {
        cout << "  Object count = " << count << endl;
    }

private:
    int x, y;
    static int count;
};

int Point::count = 0;

int main() {
    void (*funcPtr)() = Point::showCount;

    Point a(4, 5);
    cout << "Point A: " << a.getX() << ", " << a.getY();
    funcPtr();

    Point b(a);
    cout << "Point B: " << b.getX() << ", " << b.getY();
    funcPtr();

    return 0;
}

 

C++静态成员变量和静态成员函数指针

原文:https://www.cnblogs.com/lyt888/p/12493417.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!