C++:初始化列表

定义:

     C++中构造函数与其他函数不同的是,除了有名字,参数列表和函数体之外,还可以有初始化列表。
     初始化列表以冒号开头,后跟一系列以逗号分隔的初始化字段。

使用原因:

其一 主要是性能问题,对于内置类型,如int, float等,使用初始化类表和在构造函数体内初始化差别不是很大,但是对于类类型来说,最好使用初始化列表,为什么呢?

#include<iostream>
using namespace std;

class Test1
{
public:
	Test1() // 无参构造函数
	{
		cout << "无参构造" << endl;
	}
	Test1(const Test1& t1) // 拷贝构造函数
	{
		cout << "拷贝构造" << endl; 
		this->a = t1.a;
	}
	Test1& operator = (const Test1& t1) // 赋值运算符的重载函数
	{
		cout << "赋值运算符" << endl; 
		this->a = t1.a; 
		return *this;
	}
	~Test1()
	{
		cout << "析构" << endl;
	}
	int a;
};


class Test2//Test2是个测试类,它以Test1的对象为成员
{
public:
	Test1 test1;
	Test2(Test1& t1)
	{
		test1 = t1;//对test1进行赋值
	}
};


int main()
{
	Test1 t1;
	Test2 t2(t1);
	return 0;
}

运行结果:

如果使用初始化列表来实现Test2的构造函数

class Test2//Test2是个测试类,它以Test1的对象为成员
{
public:
	Test1 test1;
	Test2(Test1& t1):test1(t1)
	{
		
	}
};

int main()
{
	Test1 t1;
	Test2 t2(t1);
	return 0;
}

运行结果:

从中比较得知:使用初始化列表少了一次调用默认构造函数的过程,直接调用拷贝构造函数初始化test1,省去了调用调用默认构造函数的过程。这个这对于数据密集型的类来说是高效的。

其二 有些时候初始化列表是不可或缺的,以下情况时必须使用初始化列表。

A  当类成员中含有一个const对象时,或者是一个引用时,必须要通过成员初始化列表进行初始化,因为这两种对象要在声明后马上初始化,而在构造函数中,做的是对他们的赋值,这样是不被允许的。

1.常量成员,因为常量只能初始化不能赋值,必须放在初始化列表里面

#include<iostream>
using namespace std;

class Test
{
public:
	Test(int a, int b)
	{
		ma = a;
		mb = b;
	}
	void Show()
	{
		cout << "ma:" << ma << endl;
		cout << "mb:" << mb << endl;
	}
private:
	
	int ma;
	const int mb;
};

int main()
{
	Test test1(10,20);
	test1.Show();
	return 0;
}

C++中const修饰的成员变量一定要在初始化列表中进行初始化

Test(int a, int b)
    {
        ma = a;
        mb = b;
    }

这里相当于

const int mb;

mb=b;//常量不可以修改

解决方法:

Test(int a, int b):mb(b)
{
	ma = a;
	//mb = b;
}

这里相当于

const int mb=b;

打印结果:

2.引用类型,引用必须在定义的时候初始化,并且不能重新赋值,必须写在初始化列表里。

#include<iostream>
using namespace std;

class Test
{
public:
	Test(int a, int b)
	{
		ma = a;
		mb = b;
	}
	void Show()
	{
		cout << "ma:" << ma << endl;
		cout << "mb:" << mb << endl;
	}
private:

	int ma;
	int& mb;
};

int main()
{
	Test test1(10, 20);
	test1.Show();
	return 0;
}

解决方法:

Test(int a, int b):mb(b)
{
	ma = a;
	//mb = b;
}

打印结果:

B  数据成员是对象,并且这个对象只有含参数的构造函数,没有默认构造函数。因为使用初始化列表可以不必调用默认构造函数来初始化,而是直接调用拷贝构造函数初始化。

#include<iostream>
using namespace std;

class Test1
{
public:
	Test1(int a, int b)
	{
		cout << "Test1(int ,int)" << endl;
	}
private:
	int x;
	int y;
};

class Test2
{
public:
	Test2()
	{      
		cout << "Test2()" << endl;
	}
private:
	Test1 test; //声明
};

int main()
{
	Test2 t;
	return 0;
}

解释:
Test1有显示的带参数的构造函数,则无法依靠编译器生成无参构造函数的,所以没有两个int型数据,就无法创建Test1的对象。Test1类对象是Test2的成员,想要初始化这个对象test,就只能用成员初始化列表,没有其他办法将参数传递给Test类构造函数。

如果有一个类成员,它本身是一个类或者是一个结构,而这个成员它只有一个带参数的构造函数,没有默认构造函数,若要对这个类成员进行初始化,就必须调用这个类成员的带参的构造函数,如果没有初始化列表,那么将无法完成第一步,就会报错。

修改如下:

class Test2
{
public:
	Test2():test(1, 2)
	{      
		cout << "Test2()" << endl;
	}
private:
	Test1 test; //声明
};

int main()
{
	Test2 t;
	return 0;
}

 打印结果:

成员变量顺序:

#include<iostream>
using namespace std;

class Test
{
public:
	Test(int a, int b):mb(ma),ma(a)
	{
		cout << "Test(int ,int)" << endl;
	}
	void Show()
	{
		cout << "ma:" << ma << endl;
		cout << "mb:" << mb << endl;
	}
private:
	int ma;
	int mb;
};

int main()
{
	Test test1(10,20);
	test1.Show();
	return 0;
}

打印结果:

Test(int a, int b):ma(a),mb(ma)
{

}

private:
	int mb;
	int ma;

从中可以看出:

初始化列表中的执行顺序只和成员变量的声明顺序有关。

猜你喜欢

转载自blog.csdn.net/free377096858/article/details/84842400
今日推荐