zoukankan      html  css  js  c++  java
  • C++官方文档-静态成员

    #include <iostream>
    using namespace std;
    
    class Dummy
    {
        public:
            static int n;
            int x;
            Dummy()
                    : x(0)
            {
                n++;
            }
            Dummy(int xx)
                    : x(xx)
            {
                n++;
            }
            bool isitem(Dummy const&);
            Dummy& operator=(Dummy const& param);
    };
    bool Dummy::isitem(Dummy const& param)
    {
        if(this == &param)
            return true;
        return false;
    }
    Dummy& Dummy::operator=(const Dummy& param)
    {
        this->x = param.x;
        cout << "=" << endl;
        //this指向等号左边的对象
        return *this;
    }
    /**
     * In fact, static members have the same properties as non-member variables
     * but they enjoy class scope.
     * For that reason, and to avoid them to be declared several times,
     * they cannot be initialized directly in the class,
     * but need to be initialized somewhere outside it.
     * As in the previous example:
     * 实际上,静态成员和非成员变量一样,只是静态成员在class范围,因为这个原因,为了避免它们声明多次
     * 他们不能直接在类里面初始化,但是需要他们在class外被初始化,比如先前那个例子
     */
    int Dummy::n = 0;
    
    int main()
    {
        Dummy a;
        cout << a.n << endl;
        Dummy b[5];
        cout << a.n << endl;
        Dummy* c = new Dummy();
        cout << c->n << endl;
        cout << Dummy::n << endl;
        delete c;
        return 0;
    }
  • 相关阅读:
    PC-CSS-默认字体样式
    PC--CSS维护
    PC--CSS技巧
    PC--CSS优化
    PC--CSS命名
    PC--CSS常识
    Base64与MIME和UTF-7
    Base64算法与多版本加密技术
    Base64算法与MD5加密原理
    log4j的使用
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/8448493.html
Copyright © 2011-2022 走看看