using namespace std; class A { public: A() { cout << "默認無參構造函數" << endl; } #if 0 explicit A(int a) { cout << "顯示構造函數" << endl; //只要參數只有1個的構造函數【包括拷貝構造函數】,就可以在前面加explicit,代表不能直接將參數賦值【等號】給A對象 } #endif A(int a) { cout << "類型轉換構造函數" << endl;//參數只有1個且類型與類不同,默認是可隱式賦值【等號】,即implicit【不是C++關鍵字】 this->a = a; } A(double b, int a = 0) { cout << "類型轉換構造函數" << endl;//a有默認值,所以也算參數只有1個且類型與類不同 this->a = a; this->b = b; } A(const A& a) { cout << "拷貝構造函數" << endl; this->a = a.a; } A &operator = (const A &a) { cout << "運算符重載構造函數" << endl; this->a = a.a; return *this; } private: int a; double b; }; int main() { A a; A b(a); A c = 1; A d = a;//拷貝構造函數 A e;e = a; getchar(); return 0; }

