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

#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;
}

 

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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/335281

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午10:04
下一篇 2023年3月1日 下午10:04

相关推荐