构造函数--析构函数 T(int xx) { x=xx; cout<<"construtor: "<<x<<endl; }
#include <iostream>
using namespace std;
class T
{
int x;
public:
T(int xx) { x=xx; cout<<"construtor: "<<x<<endl; }
~ T() { cout<<"destrutor: " <<x<<endl; }
};
int main()
{
T t1(10),t2(99);
cout<<"**********************\n";
return 0;
}
构造函数 析构函数 拷贝构造函数
#include <iostream>
using namespace std;
class T
{
int x;
public:
T(int xx) { x=xx; cout<<"construtor: "<<x<<endl; }
~ T() { cout<<"destrutor: " <<x<<endl; }
};
int main()
{
T t1(10),t2(t1);
cout<<"**********************\n";
return 0;
}
#include <iostream>
using namespace std;
class T
{
int x;
public:
T(int xx) { x=xx; cout<<"construtor: "<<x<<endl; }
T(T &t) { x=t.x; cout<<"Copy construtor: "<<x<<endl; }
~ T() { cout<<"destrutor: "<<x<<endl; }
};
int main()
{
T t1(10),t2(t1);
cout<<"**********************\n";
return 0;
}


拷贝构造函数 T(T &t) { x=t.x+8; cout<<"Copy construtor: "<<x<<endl; }
#include <iostream>
using namespace std;
class T
{
int x;
public:
T(int xx) { x=xx; cout<<"construtor: " <<x<<endl; }
T(T &t) { x=t.x+8; cout<<"Copy construtor: "<<x<<endl; }
~ T() { cout<<"destrutor: " <<x<<endl; }
};
int main()
{
T t1(10),t2(t1);
cout<<"**********************\n";
return 0;
}
#include <iostream>
using namespace std;
class T
{
int *p;
public: // 给 构造函数 分配空间
T(int xx) { p=new int; *p=xx; cout<<"construtor: " <<*p<<endl; }
~ T() { cout<<"destrutor: " <<*p<<endl; }
};
int main()
{
T t1(10),t2(t1);
cout<<"**********************\n";
return 0;
}
释放 析构函数 空间 CFree 可以 ----- VC 不行
#include <iostream>
using namespace std;
class T
{
int *p;
public:
T(int xx) { p=new int; *p=xx; cout<<"construtor: " <<*p<<endl; }
~ T() { cout<<"destrutor: " <<*p<<endl; delete p; }
};
int main()
{
T t1(10),t2(t1);
cout<<"**********************\n";
return 0;
}
分配 和 释放 构造函数-- 拷贝构造函数 --析构函数
#include <iostream>
using namespace std;
class T
{
int *p;
public:
T(int xx) { p=new int; *p=xx; cout<<"construtor: " <<*p<<endl; }
T(T &t) { p=new int; *p=*(t.p); cout<<"Copy construtor:" <<*p<<endl; }
~ T() { cout<<"destrutor: " <<*p<<endl; delete p; }
};
int main()
{
T t1(10),t2(t1);
cout<<"**********************\n";
return 0;
}
没有 delete p; 也可以