菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#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;
}

 

posted on 2018-02-14 16:35  好吧,就是菜菜  阅读(134)  评论(0编辑  收藏  举报