[問題] 二維陣列default constructor問題
開發平台(Platform): (Ex: Win10, Linux, ...)
Win7
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
Visual studio 2013
問題(Question):
要建一個class matrix
能夠透過constructor生成二維陣列
有無參數跟有參數兩種constructor
class matrix
{
private:
int** m;
int size;
public:
//parameter constructor
matrix(int s)
{
size = s;
m = new int*[size];
for (int i = 0; i<size; i++)
m[i] = new int[size];
}
//default constructor
matrix()
{
m = new int*[size];
for (int i = 0; i<size; i++)
m[i] = new int[size];
}
}
-----------------------------------------
在主程式裡頭不曉得要怎麼達到下面這種效果
matrix* mArr1 = new matrix[10]; //calling default constructor
matrix mArr2[5]; //calling default constructor
資料成員雖然有size可是沒初值用在default constructor上面無作用
要做像是 matrix mArr2[5]這樣事情時就無法
感覺matrix(){}有點像是多打的
想了很久不知道怎麼解決
C++新手請求各位高手幫忙@@
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.241.16.145
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1478348296.A.FCD.html
推
11/05 21:00, , 1F
11/05 21:00, 1F
→
11/05 21:01, , 2F
11/05 21:01, 2F
推
11/05 21:04, , 3F
11/05 21:04, 3F
→
11/05 21:04, , 4F
11/05 21:04, 4F
→
11/05 21:05, , 5F
11/05 21:05, 5F
→
11/05 21:59, , 6F
11/05 21:59, 6F
→
11/05 21:59, , 7F
11/05 21:59, 7F
→
11/05 22:00, , 8F
11/05 22:00, 8F
→
11/05 22:01, , 9F
11/05 22:01, 9F
→
11/05 22:02, , 10F
11/05 22:02, 10F
→
11/05 22:02, , 11F
11/05 22:02, 11F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章