/****** 1.构造函数初始化列表******/
#include<iostream>
using namespace std;
class Test
{
public:
Test():p(new int[size]),size(10)//初始化列表初始化变量size和指针p,注意顺序
{
cout<<"new obj creat succes!"<<endl;
for(int i=1;i<=size;i++)
p[i]=i;
cout<<"size = "<<size<<endl;
for(int i=1;i<=size;i++)
cout<<p[i]<<" ";cout<<endl;
}
~Test()
{
cout<<"obj has been destoryed!"<<endl;
}
private: //这里若是先定义*P后定义size好像对程序运行没影响
int size; //因为如果先定义*p,那new运算符就不知道该为*p分配多少空间了
int *p; //这点我很好奇。
};
int main()
{
Test a;
return 0;
}
程序结果:
利用构造函数初始化列表初始化了size和指针*p,然后对指针p所指的区域赋值,最后打印并撤销对象。
/****** 2.构造函数初始化列表与静态变量******/
#include<iostream>
using namespace std;
int a=8;
int b=6;
int c=7;
class Test
{
public:
Test():p(&a)//让p指向a
{
cout<<"new obj creat succes!"<<endl;
for(int i=0;i<3;i++)
p[i]=i;//对指针所指区域赋值
for(int i=0;i<3;i++)
cout<<p[i]<<" ";cout<<endl;
}
~Test()
{
cout<<"obj has been destoryed!"<<endl;
}
private:
int size;
int *p;
};
int main()
{
Test s;
cout<<"a= "<<a<<" and,a address is "<<&a<<endl;//打印a,b,c的值和地址
cout<<"b= "<<b<<" and,b address is "<<&b<<endl;
cout<<"c= "<<c<<" and,c address is "<<&c<<endl;
return 0;
}
程序结果:
这里a,b,c是静态变量,他们在分配时的地址是连续的,初始化*p时让p指向a,让后修改p指向的内容,程序运行结果可以看到
a,b,c的值发生了改变,但地址是连续的。
/****** 3.对象成员初始化顺序******/
#include<iostream>
using namespace std;
class Test1
{
public:
Test1()
{
cout<<"Test1 obj creat succes!"<<endl;
}
~Test1()
{
cout<<"Test1 obj has destoryed!"<<endl;
}
private:
int size;
};
class Test2
{
public:
Test2()
{
cout<<"Test2 obj creat succes!"<<endl;
}
~Test2()
{
cout<<"Test2 obj has destoryed!"<<endl;
}
private:
Test1 num;//Test2对象的私有成员是Test1的一个对象
};
int main()
{
Test2 s;
return 0;
}
程序运行结果:
创建对象Test2 s时,对象s的私有成员num被创建,然而num是Test1的一个对象,所以Test1的构造函数先被调用
然后Test2的构造函数被调用,析构恰好与之相反。/*********4.多对象的构造函数、析构函数顺序***/
#include<iostream>
using namespace std;
class Test1
{
public:
Test1(int nu):num(nu)
{
cout<<"num = "<<num<<endl;
cout<<num<<" creat success!"<<endl;
}
~Test1()
{
cout<<num<<" has destoryed!"<<endl;
}
private:
int num;
};
class Test2
{
public:
Test2():obj2(13),obj1(12)//带参数初始化Test1的两个对象
{
cout<<"Test2 obj creat succes!"<<endl;
}
~Test2()
{
cout<<"Test2 obj has destoryed!"<<endl;
}
private:
Test1 obj1;//Test2对象的私有成员是Test1的对象
Test1 obj2;//构造函数的顺序与定义顺序有关,与初始化无关
};
int main()
{
Test2 s;
return 0;
}
程序运行结果:
可以看出,Test1的obj1先被创建,obj2后被创建;
/*******5.对象的构造按定义的顺序,与初始化顺序无关****/
#include<iostream>
using namespace std;
class Test1
{
public:
Test1(int nu):num(nu)
{
cout<<"num = "<<num<<endl;
cout<<num<<" creat success!"<<endl;
}
~Test1()
{
cout<<num<<" has destoryed!"<<endl;
}
private:
int num;
};
class Test2
{
public:
Test2(int n,int p):obj2(p),obj1(n)
{
cout<<"Test2 obj creat succes!"<<endl;
}
~Test2()
{
cout<<"Test2 obj has destoryed!"<<endl;
}
private:
Test1 obj1;//Test2对象的私有成员是Test1的一个对象
Test1 obj2;//注意定义的顺序,对象的构造按定义的顺序,与初始化顺序无关
};
int main()
{
Test2 s(35,36);
return 0;
}
运行结果:
/******6.对象中常量的初始化只能在初始化列表中******/
#include<iostream>
using namespace std;
class Test1
{
public:
Test1()
{
num=14;//常量的赋值不是以初始化列表形式,不被允许
cout<<"num = "<<num<<endl;
cout<<num<<" creat success!"<<endl;
}
~Test1()
{
cout<<num<<" has destoryed!"<<endl;
}
private:
const int num;
};
int main()
{
Test1 s;
return 0;
}
程序运行结果:
程序编译不通过
/****** 7.对象中常量的初始化只能在初始化列表中******/
#include<iostream>
using namespace std;
class Test1
{
public:
Test1():num(14)//初始化列表初始化const常量
{
cout<<"num = "<<num<<endl;
cout<<num<<" creat success!"<<endl;
}
~Test1()
{
cout<<num<<" has destoryed!"<<endl;
}
private:
const int num;//const变量num
};
int main()
{
Test1 s;
return 0;
}
运行结果:
/****** 8..对象中引用成员的初始化也只能在初始化列表中******/
#include<iostream>
using namespace std;
class Test1
{
public:
Test1():num(14)
{
nu=num; //非初始化列表赋值引用型变量,不允许
cout<<"num = "<<num<<endl;
cout<<num<<" creat success!"<<endl;
cout<<"nu = "<<nu<<endl;
cout<<nu<<" creat success!"<<endl;
}
~Test1()
{
cout<<num<<" has destoryed!"<<endl;
}
private:
int num;
int& nu;
};
int main()
{
Test1 s;
return 0;
}
编译出错:
/******9. 对象中引用成员的初始化也只能在初始化列表中******/
#include<iostream>
using namespace std;
class Test1
{
public:
Test1():num(14),nu(num)//初始化列表初始化引用变量
{
//nu=num;
cout<<"num = "<<num<<endl;
cout<<num<<" creat success!"<<endl;
cout<<"nu = "<<nu<<endl;
cout<<nu<<" creat success!"<<endl;
}
~Test1()
{
cout<<num<<" has destoryed!"<<endl;
}
private:
//const int num;编译报错,不能将常量赋值给引用,纳闷
Int num;
int& nu;
};
int main()
{
Test1 s;
return 0;
}
运行结果:
可以看到,程序中用初始化列表定义了num变量,然后将num变量赋值给引用型变量nu,值得注意的是
如果num声明为const,编译不通过,即不允许这样的赋值操作。