帶有指針型數(shù)據(jù)成員的類新手初學(xué)C++心得
作者: 來源: 發(fā)布時間:2011-3-15 16:50:37 點擊:
該類中為指針數(shù)據(jù)成員分配堆內(nèi)存的成員函數(shù)


void stringprocess::test()
{
indexes=new int[5];
}
{
indexes=new int[5];
}
主函數(shù)中測試調(diào)用:
int _tmain(int argc, _TCHAR* argv[])
{
stringprocess p;
p.test();
cout<<"finish"<<endl;
int f;
cin>>f;
}
{
stringprocess p;
p.test();
cout<<"finish"<<endl;
int f;
cin>>f;
}
輸出結(jié)果:
如果主函數(shù)中不調(diào)用p的test函數(shù)則運(yùn)行結(jié)果如下:
鑒于一般的C++入門課本僅提到“在構(gòu)造函數(shù)中動態(tài)分配堆內(nèi)存,在析構(gòu)函數(shù)中釋放”,以上內(nèi)容僅供C++初學(xué)者參考之,避免造成思維固化。
上一篇:C#代碼反編譯 得到項目可運(yùn)行源碼 經(jīng)驗分享 下一篇:
[收藏此文章]