004010AD push 4
004010AF call operator new (00401300)
004010B4 add esp,4
004010B7 mov dword ptr [ebp-18h],eax
004010BA mov dword ptr [ebp-4],0
004010C1 cmp dword ptr [ebp-18h],0
004010C5 je process+56h (004010d6)
004010C7 push 0
004010C9 mov ecx,dword ptr [ebp-18h]
004010CC call @ILT+80(test::test) (00401055)
004010D1 mov dword ptr [ebp-1Ch],eax
004010D4 jmp process+5Dh (004010dd)
004010D6 mov dword ptr [ebp-1Ch],0
004010DD mov eax,dword ptr [ebp-1Ch]
004010E0 mov dword ptr [ebp-14h],eax
004010E3 mov dword ptr [ebp-4],0FFFFFFFFh
004010EA mov ecx,dword ptr [ebp-14h]
004010ED push ecx
004010EE lea ecx,[ebp-10h]
004010F1 call @ILT+65(auto_point::auto_point) (00401046)
004010F6 mov dword ptr [ebp-4],1
23: if(1/* some errors happens */)
004010FD mov edx,1
00401102 test edx,edx
00401104 je process+97h (00401117)
24: {
25: return;
00401106 mov dword ptr [ebp-4],0FFFFFFFFh
0040110D lea ecx,[ebp-10h]
00401110 call @ILT+75(auto_point::~auto_point) (00401050)
00401115 jmp process+0A6h (00401126)
26: }
27:
28: return;
00401117 mov dword ptr [ebp-4],0FFFFFFFFh
0040111E lea ecx,[ebp-10h]
00401121 call @ILT+75(auto_point::~auto_point) (00401050)
29: }
大家可以从上面的代码看得很清楚,不管代码在什么时候退出,函数都会利用类的基本特性,自动调用类的析构函数,那么进而内存就会得到释放,不会发生内存泄露的问题。但是我们发现上面的解决方案也有不足的地方,就是每一个类都需要额外定义一个额外的定义类。那么有没有什么办法解决这一问题呢?那就是模板了。
template
class auto_point
{
type* t;
public:
auto_point(type* p) : t(p) {}
~auto_point() { if(t) delete t;}
test* operator->() { return t;}
const test& operator* () {return *t;}
};
template
class auto_point
{
type* t;
public:
auto_point(type* p) : t(p) {}
~auto_point() { if(t) delete t;}
test* operator->() { return t;}
const test& operator* () {return *t;}
}; 如果我们的类型不是特定的,那么我们只需要在使用的时候按照特定的类型输入即可
w Roman"'>)尝试编写一个二分法的泛型处理函数?
(3)尝试编写一个quick_sort的泛型函数,可能考虑的因素需要多一些?不过也可以尝试一下哦。