一步一步 實現智慧指標(二)
假如我要做一個對Point類的智慧指標 。
程式碼實現
usingnamespace 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
};
MySmartPoint(constMySmartPoint & sp):count(1)
{
count ++;
cout<<"count =" <<count <<endl;
std::cout <<"const拷貝構造" <<std::endl;
}
MySmartPoint(MySmartPoint &p)
{
cout<<"count ="
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;
}
constPoint*operator->()const
{
returnptr;
}
Point &operator*()
{
return *ptr;
}
constPoint &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;
}
constPoint*operator->()const
{
returnptr;
}
Point &operator*()
{
return *ptr;
}
constPoint &operator*()const
{
return *ptr;
}
};
這樣 MySmartPoint sp3 = sp1; 就可以了接下來 解決賦值 = 的問題
過載= 即可
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>
usingnamespacestd;
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=(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;
}
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;
}
};
基本完成。