[語法] Default constructor疑問
下面是我class的片段
class Twod
{
public:
Twod();
Twod(int row, int col);
friend ostream& operator << (ostream& outstream,
const Twod& output);
private:
double **matrix;
int num_colnum;
int num_row;
};
Twod :: Twod() //預設是三乘三的單位矩陣
:num_row(3), num_colnum(3)
{
matrix = new double *[3];
for(int i = 0; i < 3; i ++)
matrix[i] = new double[3];
for(int i = 0; i < 3; i ++)
for(int j = 0; j < 3; j ++)
matrix[i][j] = 0;
for(int i = 0; i < 3; i ++)
matrix[i][i] = 1;
}
//row*col的零矩陣
Twod :: Twod(int row, int col)
:num_row(row), num_colnum(col)
{
matrix = new double *[row];
for(int i = 0; i < row; i ++)
matrix[i] = new double[col];
for(int i = 0; i < row; i ++)
for(int j = 0; j < col; j ++)
matrix[i][j] = 0;
}
ostream& operator <<(ostream& outstream,
const Twod& output)
{
for(int i = 0; i < output.num_row; i ++)
{
for(int j = 0; j < output.num_colnum; j ++)
outstream << output.matrix[i][j] << " ";
outstream << endl;
}
return outstream;
}
再來是我的主程式
int main(void)
{
cout <<"The main program is bulding" << endl;
cout << "test\n";
Twod matrix1(); // <- 在這我不小心手賤加了一個()....
//應該是Twod matrix1; 這樣就可以直接呼叫 Default constructor
Twod matrix2(4, 4);
cout << matrix1 << endl;
cout << matrix2 << endl;
system("pause");
return 0;
}
結果編譯器告訴我:
[Warning] the address of `Twod matrix1()', will always evaluate as `true'
而且也沒辦法看到matrix1的資料內容
只看到1 ....
可以請版上的各位高手指點小弟一下
到底那個的小括號是甚麼意思?
--
冬天到了!
春天還會遠嗎?
-雪萊-
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 134.208.45.123
→
04/08 10:02, , 1F
04/08 10:02, 1F
似乎像是個function....
所以我這樣寫是呼叫了
一個形態為Twod 名叫matrix1的function嗎?
可是這樣寫編譯器會給過嗎?
※ 編輯: siuol 來自: 134.208.26.119 (04/08 12:12)
推
04/08 12:13, , 2F
04/08 12:13, 2F
謝謝~ 我了解了
※ 編輯: siuol 來自: 134.208.26.119 (04/08 12:17)
推
04/08 12:52, , 3F
04/08 12:52, 3F