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};  // 注意生命周期!
}
相关推荐
端平入洛1 天前
delete又未完全delete
c++
端平入洛2 天前
auto有时不auto
c++
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20213 天前
信号量和信号
linux·c++
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054963 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月3 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js