袁辉勇 4=25-2 构造函数
T t(100); cout<<"**********\n";
#include <iostream>
using namespace std;
class T
{
int x;
public:
T (int xx) { x=xx; cout<<"construct: "<<x<<endl; } //(六)
~T () { cout<<"destruct: "<<x<<endl; }
};
int main(int argc, char *argv[])
{
T t(100);
cout<<"**********\n";
return 0;
}
起点 作用 T t2(t1);
#include <iostream>
using namespace std;
class T
{
int x;
public:
T (int xx){x=xx; cout<<"construct: "<<x<<endl;} //(七)
~T () {cout<<"destruct: "<<x<<endl;}
};
int main(int argc, char *argv[])
{
T t1(100);
T t2(t1); //起点 作用
cout<<"**********\n";
return 0;
}
构造函数 拷贝构造函数
#include <iostream>
using namespace std;
class T
{
int x;
public:
T (int xx) {x=xx; cout<<"construct: "<<x<<endl; } //(八)
~T () { cout<<"destruct: "<<x<<endl; }
T(T &t) {x=t.x; cout<<"copy construct: "<<x<<endl; }
};
int main()
{
T t1(100);
T t2(t1);
return 0;
}
C++ 文章被收录于专栏
C++谭浩强 冯博强 贾应知 袁辉勇