作者Carbontube (碳管)
看板C_and_CPP
標題Re: [問題] 請問函式的引數若為另一函式,是否就不 …
時間Thu May 28 22:49:27 2009
※ 引述《littleshan (我要加入劍道社!)》之銘言:
這個問題沒有快速又簡單的解法。最簡單的解法是 Mul() 和 transpose()
都回傳 Matrix (傳值),雖然會浪費多餘的時間去複製暫時變數,但不會
造成 memory leak 或 double free 的情況。如果需要良好的效能,可能
就需要搬出 expression template 來用了。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.121.116.63
1F:→ james732:原來如此 受教了 05/28 10:23
2F:推 dcleft:推,傳值應會比傳vector好一些 05/28 12:06
3F:推 dcleft:不過請問一下 "回傳 Matrix (傳值)" 是指傳double[][] ? 05/28 14:14
4F:→ dcleft:還是傳什麼? 05/28 14:15
5F:→ mantour:寫成一個class,然後把delete寫在destructor內 05/28 20:50
我想請問大大,這matrix class實做是要怎樣才會自己free呢?
我想成這樣:
typedef class Matrix {
public :
int n, m; // size of column and row
double **element;
Matrix (int n=1, int m =1)
{/*allocate ememory of element */ } ;
~Matrix()
{/*free memory of element*/};
mul(Matrix B);
operator+(Matrix B);
:
:
} matrix;
/transpose is not a member function of class Matrix/
matrix transpose(matrix A)
若是這樣想不出來那裡建解構啊。。。
還請大大詳細解釋 謝謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.167.74.111
※ 編輯: Carbontube 來自: 218.167.74.111 (05/28 22:52)