之前在總結c++智慧型指標的時候提到過這麼乙個問題:對於shared_ptr,如果不是new出來的物件如何通過智慧型指標管理呢?其實shared_ptr設計了乙個刪除器來解決這個問題。
簡單的實現乙個有定置刪除器的shared_ptr//仿函式的刪除器
template
struct freefunc };
template
struct deletearrayfunc };
intmain()
};intmain()
//執行結果 >>>helloworld!
struct fclose};
struct free};
//預設刪除器是delete
struct defaultdel};
template
class sharedptr
//採用引用計數,實現乙個可以有多個指標
//指向同一塊記憶體的類模板,sharedptr是類模板,不是智慧型指標型別
private:
void
release()
}private:
t* _ptr;
int* _pcount;
d _del;};
//建構函式
template
sharedptr::
sharedptr
(t* ptr,d del)
:_ptr
(ptr)
,_pcount
(new int(1
)),_del
(del)
//拷貝建構函式
template
sharedptr::
sharedptr
(const sharedptr
& sp)
//operator=
template
sharedptr
& sharedptr
::operator=
(sharedptr sp)
//operator *
template
t& sharedptr
::operator*()
//operator ->
template
t* sharedptr
::operator->()
//析構函式
template
sharedptr::
~sharedptr()
//測試用例
void
test()
intmain()
python 仿函式 C 仿函式
c 的標準庫stl裡面有6大部件,其中之一為仿函式。初始看到這一名字可能讓人摸不著頭腦 函式倒是挺容易理解,何故又起個仿函式的名字呢?本文將帶你揭開它看起來挺讓人迷惑但是實際上很簡單的面紗。仿函式,看名字就知道它肯定和函式有什麼關聯,但是也肯定和函式有什麼區別。函式主要是一塊接收輸入引數然後按照一定...
仿函式 C 中仿函式的應用
仿函式 c 中仿函式的應用 在使用仿函式的時候,主要用到以下兩種 一種是以基類std unary function派生出來的派生類 另一種是以基類std binary function派生出來的派生類。而這兩種有什麼區別呢?它們之間的區別只是第一種接收的引數個數為乙個,而第二種接收的引數的個數為兩個...
c for each 與仿函式
for each有乙個獨門絕技,其他演算法沒有,那就是可以返回值來獲取函式的狀態 include include include using namespace std class meanvalue void operator int elem double value int main mean...