1.編譯器自己會,產生
2.可以自己寫
3.是一個像middleware和Log的傢伙
4.
class C{
public:
int v;
C();
C(const C& c){
v=c.v;
cout<<"copy";
}
};
C::C(){
}
int main(int argc, char** argv) {
C c;
c.v=5;
C a(c);
cout<<a.v;
}
5.會調用有3個時機
5.1 C a(c)
5.2 方法裡有C的物件型態 如 test(C c)
5.3動態建立
如C *p=new C(c);
6.賦值構造函數
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class C{
public:
int v;
C(){
}
C(const C& c){
v=c.v;
cout<<"copy";
}
void haha(C c){
cout<<"call haha";
}
C& operator =(const C& c){
cout<<"operator";
}
};
int main(int argc, char** argv) {
C p;
C c=p; //會print copy
c=p; //會print operator
}
6.1就只是=號的複製