先考慮一種情況,對一個已知對象進行拷貝,編譯系統(tǒng)會自動調用一種構造函數(shù)——拷貝構造函數(shù),如果用戶未定義拷貝構造函數(shù),則會調用默認拷貝構造函數(shù)。
//main.cpp #include <iostream> #include "student.h" int main() { Student s1; Student s2(s1);//Student s2 = s1;//復制對象 return 0; }
//student.h #ifndef STUDENT_H