class A{
friend ostream& operator<<(ostream& out, A* p);
private:
int a;
char* b;
static A* singleA;
private:
A(int a, char* b){
this->a = a;
this->b = new char[10];
strcpy(this->b, b);
}
~A(){
delete this->b;
this->b = NULL;
}
public:
static A* getsingleA(){
return singleA;
}
A* operator++(){
a++;
return this;
}
};
A* A::singleA = new A(10, "abc");
ostream& operator<<(ostream& out, A* p){
out<<p->a<<" "<<p->b<<endl;
return out;
}
int main()
{
A* p = A::getsingleA();
cout<<p<<endl;
cout<<++(*p)<<endl;
cout<<++p<<endl;
return 0;
}
输出结果为
原因:由于采用单例模式,使用类的指针来进行类的操作,对于第三个输出,是因为++p并没有对类对象进行++,而是对类对象的指针进行了++