explicit和initializer_list

1、在传统用法中,explicit用于单参构造函数,阻止隐式类型转换。

cpp 复制代码
class MyClass
{
public:
    explicit MyClass(int x){}//防止隐式转换
};
void func(MyClass obj){}
int main()
{
    MyClass obj1(5);
    MyClass obj2 = 5;//错误,禁止隐式类型转换
    MyClass obj3 = MyClass(5);

    func(5);//错误,禁止隐式类型转换
    func(MyClass(5));正确
}

2、在C++11之后,允许使用在多参构造函数,阻止一致性初始化来隐式转换。

cpp 复制代码
class Point
{
public:
	explicit Point(int x, int y) :x(x), y(y) {}
private:
	int x, y;
};
void drawPoint(Point p)
{
}
int main()
{
	Point p1(1, 2);//OK 直接初始化
	Point p2 = { 1,2 };//ERROR explicit 阻止一致性初始化隐式转换
	Point p3 = Point(1, 2);//OK 显式调用构造函数

	drawPoint({ 1,2 });//ERROR explicit 阻止一致性初始化隐式转换
	drawPoint(Point(1, 2));//OK 显式调用构造函数
	return 0;
}

3、std::initializer_list是C++引入的轻量级容器,使用{}来初始化对象,用在相同类型不定数量的一致性参数初始化,比如想写一个不定个数的打印函数(变参模板功能更强大,不仅是参数个数不定,而且参数类型也可以不一致,而initializer_list要求必须相同类型的参数个数不定)。

cpp 复制代码
#include <initializer_list>
#include <iostream>
#include <string>

void printList(std::initializer_list<std::string> strs)
{
	for (auto str : strs)
	{
		std::cout << str << std::endl;
	}
}
int main()
{
	printList({ "Hello", "World", "from", "C++14" });
	printList({ "forTest1","forTest2" });
	return 0;
}

4、通常在构造函数中使用,用来创建支持一致性初始化的类。

cpp 复制代码
#include <initializer_list>
#include <iostream>
#include <string>
#include <vector>

class MyContainer
{
private:
	std::vector<int> data;
public:
	//使用一致性初始化构造函数
	MyContainer(std::initializer_list<int> init) :data(init) {}
	//使用常规构造函数
	MyContainer(int size, int value = 0) :data(size, value) {}
	void print()const
	{
		for (const auto& elem : data)
		{
			std::cout << elem << " ";
		}
		std::cout << std::endl;
	}
};
int main()
{
	MyContainer c1{ 1, 2, 3, 4, 5 }; // 调用一致性初始化构造函数
	MyContainer c2 = { 6, 7, 8, 9, 10 }; // 调用一致性初始化构造函数
	MyContainer c3(5, 10);           // 调用常规构造函数
	c1.print(); // 输出: 1 2 3 4 5
	c2.print(); // 输出: 10 10 10 10 10
	return 0;
}

5、也可以用在返回一致性初始化列表。

cpp 复制代码
#include <vector>

// 返回初始化列表(转换为其他容器)
std::vector<int> createVector() {
    return {1, 2, 3, 4, 5};  // 隐式转换
}

// 返回初始化列表自身
auto getInitList() -> std::initializer_list<int> {
    return {10, 20, 30, 40};  // 注意生命周期!
}
相关推荐
zjun10011 小时前
C++:单例模式
c++·单例模式
-银雾鸢尾-5 小时前
C#中的拓展方法
开发语言·c#
guodingdingh7 小时前
软件开发工作问题总结0718
java·开发语言·数据库
优橙教育9 小时前
5G网优培训 vs Java开发:转行选哪个?
java·开发语言·5g
同勉共进10 小时前
记一例 vibe coding + gcc bug 导致的线程池死锁问题
c++·线程池·gcc·死锁·vibe coding
我叫洋洋10 小时前
C ++ [ hello world ]
c语言·c++·算法
SeaTunnel10 小时前
从 Python Script 地狱到标准化数据集成框架
大数据·开发语言·python·程序员·代码·seatunnel
碎光拾影11 小时前
ARM交叉工具链各工具作用及IMX6ULL平台LED+蜂鸣器裸机程序实现
java·开发语言·数据库
Marst Code11 小时前
(python)2026Plotly 库评估:交互式可视化到底值不值得引入?
开发语言·python
Miao1213112 小时前
微服务 API 测试实践:海外某民宿平台如何构建模式驱动测试基础设施
java·开发语言