假如我要做一个对Point类的智能指针 。
代码实现
using namespace std;
class Point
{
private:
//public:
int x;
int y;
public:
Point(int xVal,int yVal):x(xVal),y(yVal)
{
}
~Point()
{
}
int getX()
{
returnx;
};
int getY()
{
returny;
}
};
//封装的智能指针类
class MySmartPoint
{
public:
// MySmartPoint(){};
MySmartPoint(Point *p):count(1)
{
cout<<"count =" <<count <<endl;
this->ptr = p;
};
MySmartPoint(int x,int y):count(1)
{
cout<<"count =" <<count <<endl;
this->ptr =newPoint(x,y);
};
MySmartPoint(constMySmartPoint & sp):count(1)
{
count ++;
cout<<"count =" <<count <<endl;
std::cout <<"const拷贝构造" <<std::endl;
}
MySmartPoint(MySmartPoint &p)
{
cout<<"count =" <<count <<endl;
std::cout <<"拷贝构造" <<std::endl;
};
~MySmartPoint()
{
count --;
cout<<"析构 count= "<<count <<endl;
if (0 ==count)
{
delete this->ptr;
}
}
MySmartPoint& operator=(constMySmartPoint &p)
{
return *this;
}
Point*operator->()
{
returnptr;
}
const Point*operator->()const
{
returnptr;
}
Point &operator*()
{
return *ptr;
}
const Point &operator*()const
{
return *ptr;
}
//private:
Point *ptr;
int count;//记录ptr个数
};
void fun1()
{
Point *p =newPoint(1,2);
MySmartPoint sp =MySmartPoint(p);
cout<<"x = "<<sp->getX()<<endl ;
MySmartPoint sp1 =MySmartPoint(newPoint(3,4));
cout<<"x = "<<sp1->getX()<<endl ;
MySmartPoint sp2 =MySmartPoint(5,6);
cout<<"x = "<<sp2->getX()<<endl ;
// MySmartPoint sp3 = sp1;
}
int main()
{
fun1();
cout<<"over"<<endl;
}
能够解决 new delete 对称,但无法对指针的引用计数 所以fun1 最后一行 crash
因为每次创建的新对象count都是新的。
怎样才能保证count的同一个count呢?
基本原则,封装成一个新类,作为MySmartPointsmart的一个指针成员,这样就能保证count是同一个对象了。
对于Point 对象应该封装在哪里?暂时,没有想法。先放在MySmartPoint 里吧。
修改之后的 代码如下。
class MySmartPoint;
class Counter
{
public:
int count;
};
class MySmartPoint
{
//private:
Point *ptr;
Counter *countPtr;//记录ptr个数
public:
// MySmartPoint(){};
MySmartPoint(Point *p)
{
countPtr =newCounter();
countPtr ->count =1;
cout<<"count =" <<countPtr ->count <<endl;
this->ptr = p;
};
MySmartPoint(int x,int y)
{
countPtr =newCounter();
countPtr ->count =1;
cout<<"count =" <<countPtr ->count <<endl;
this->ptr =newPoint(x,y);
};
MySmartPoint(constMySmartPoint & sp)
{
sp.countPtr->count ++;
countPtr = sp.countPtr;
ptr = sp.ptr;
cout<<"count =" <<countPtr ->count <<endl;
std::cout <<"const拷贝构造" <<std::endl;
}
MySmartPoint(MySmartPoint &sp)
{
sp.countPtr->count ++;
countPtr = sp.countPtr;
ptr = sp.ptr;
cout<<"count =" << sp.countPtr ->count <<endl;
std::cout <<"拷贝构造" <<std::endl;
};
~MySmartPoint()
{
countPtr ->count --;
cout<<"析构 count= "<<countPtr ->count <<endl;
if (0 == countPtr -> count)
{
delete this->ptr;
cout<<"delete point is "<<this->ptr<<endl;
}
}
MySmartPoint& operator=(constMySmartPoint &p)
{
this->countPtr = p.countPtr;
std::cout <<" operator=" <<std::endl;
return *this;
}
Point*operator->()
{
returnptr;
}
const Point*operator->()const
{
returnptr;
}
Point &operator*()
{
return *ptr;
}
const Point &operator*()const
{
return *ptr;
}
};
接下来 解决赋值 = 的问题
重载= 即可
MySmartPoint& operator=(constMySmartPoint &p)
{
if (this == &p)
{
return *this;
}
p.countPtr->count ++;
countPtr->count --;
if (countPtr->count ==0)
{
delete this->ptr;
cout<<"delete point is "<<this->ptr<<endl;
}
countPtr = p.countPtr;
ptr = p.ptr;
std::cout <<" operator=" <<std::endl;
return *this;
}
这样 sp3 = sp;就可以了。
自此主要的功能基本完成。一下再做一些 优化。
用泛型思想。模板化
#include <stdio.h>
#include <iostream>
using namespace std;
class Counter
{
public:
int count;
};
template <class T>
class MySmartPoint
{
//private:
T *ptr;
Counter *countPtr; //记录ptr 个数
public:
MySmartPoint(){};
MySmartPoint(T *p)
{
countPtr = new Counter();
countPtr -> count =1;
cout<< "count =" << countPtr -> count <<endl;
this->ptr = p;
};
MySmartPoint(int x,int y)
{
countPtr = new Counter();
countPtr -> count =1;
cout<< "count =" << countPtr -> count <<endl;
this->ptr = new T;
};
MySmartPoint(const MySmartPoint & sp)
{
sp.countPtr->count ++;
countPtr = sp.countPtr;
ptr = sp.ptr;
cout<< "count =" << countPtr -> count <<endl;
std::cout << "const 拷贝构造" << std::endl;
}
MySmartPoint(MySmartPoint &sp)
{
sp.countPtr->count ++;
countPtr = sp.countPtr;
ptr = sp.ptr;
cout<< "count =" << sp.countPtr -> count <<endl;
std::cout << " 拷贝构造" << std::endl;
};
~MySmartPoint()
{
countPtr -> count --;
cout<<"析构 count= "<<countPtr -> count <<endl;
if (0 == countPtr -> count)
{
delete this->ptr;
cout<< "delete point is "<<this->ptr<<endl;
}
}
MySmartPoint& operator=(const MySmartPoint &p)
{
if (this == &p) {
return *this;
}
p.countPtr->count ++;
countPtr->count --;
if (countPtr->count == 0)
{
delete this->ptr;
cout<< "delete point is "<<this->ptr<<endl;
}
countPtr = p.countPtr;
ptr = p.ptr;
std::cout << " operator=" << std::endl;
return *this;
}
T* operator->()
{
return ptr;
}
const T* operator->()const
{
return ptr;
}
T &operator*()
{
return *ptr;
}
const T &operator*()const
{
return *ptr;
}
int main()
{
Point *p = new Point(3,4);
MySmartPoint<Point> sp = MySmartPoint<Point>(p);
cout<< sp->getX()<<endl;
cout<< "over"<<endl;
}
};