#include <iostream>
using namespace std; class Dummy
{
public:
static int n;
int x;
Dummy()
: x()
{
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 = ; int main()
{
Dummy a;
cout << a.n << endl;
Dummy b[];
cout << a.n << endl;
Dummy* c = new Dummy();
cout << c->n << endl;
cout << Dummy::n << endl;
delete c;
return ;
}
05-11 15:20