拷貝構造函數
阿新 • • 發佈:2018-01-13
post body 分享 class 定義 ron pri 自動 拷貝
例如類: class Student{
public:
Student(){
cout<<"student"<<endl;
}
};
定義:Student stu1; //這個三個實例化中,只有第一個會打印student,即執行構造函數
Student stu2=stu1; //另外的兩個執行的是拷貝構造函數,不會打印student
Student stu3(stu1);
拷貝構造函數: //註:可以有返回值,但是不可以重載;
class Student{
public:
Student(){m_name="jo";}
Student(const Student& stu){} //未定義則系統自動默認一個,即賦值用
//即實例Student stu作為 =賦值 或者 ()賦值時,會自動調用拷貝 構造函數;
//而定義stu的時候(Student stu)會自動調用 構造函數
Private:
string m_name;
};
拷貝構造函數