實(shí)際上,當(dāng)我們定義一個class而沒有明確定義構(gòu)造函數(shù)的時候,
編譯器會自動假設(shè)兩個重載的構(gòu)造函數(shù)
(默認(rèn)構(gòu)造函數(shù)"default constructor" 和復(fù)制構(gòu)造函數(shù)"copy constructor")。
例如,對以下class:
class CExample {
public:
int a,b,c;
void multiply (int n, int m) { a=n; b=m; c=a*b; };
};
沒有定義構(gòu)造函數(shù),
編譯器自動假設(shè)它有以下constructor 成員函數(shù):
必須注意:這兩個默認(rèn)構(gòu)造函數(shù)(empty construction 和 copy constructor )
只有在沒有其它構(gòu)造函數(shù)被明確定義的情況下才存在。
如果任何其它有任意參數(shù)的構(gòu)造函數(shù)被定義了,這兩個構(gòu)造函數(shù)就都不存在了。
在這種情況下,
如果你想要有empty construction 和 copy constructor ,
就必需要自己定義它們。