当前位置: 首页 > news >正文

列表初始化

一、统一的初始化

#include <iostream>
using namespace std;class Test
{
public:Test(int) {}
private:Test(const Test &);
};// 数组的初始化
int array[] = { 1,3,5,7,9 };
double array1[3] = { 1.2, 1.3, 1.4 };// 对象的初始化
struct Person
{int id;double salary;
}zhang4{ 1, 4000 };int main(void)
{Test t1(520);Test t2 = 520; Test t3 = { 520 };Test t4{ 520 };int a1 = { 1314 };int a2{ 1314 };int arr1[] = { 1, 2, 3 };int arr2[]{ 1, 2, 3 };return 0;
}
  • t1 通过有参构造实现了初始化
  • t2 语法错误,提供的拷贝构造是私有的。如果拷贝构造函数是公共的,520会通过隐式类型转换被 Test(int)构造成一个匿名对象,然后再通过这个匿名对象进行拷贝构造得到t2
  • t3 t4 使用了初始化列表的方式   效果与t1相同  
  • t4、a2、arr2的写法,是C++11中新添加的语法格式,使用这种方式可以直接在变量名后边跟上初始化列表,来进行变量或者对象的初始化。

二、列表初始化细节

1.聚合体

  • 普通数组本身可以看做是一个聚合类型
  • int x[] = {1,2,3,4,5,6};
    double y[3][3] = { {1.1,2.2,3.3}, {4.4,5.5,6.6}, {7.7,8.8,9.9}};
    char array[] = {'a','b','c','d','e'};
    std::string sarry[] = {"hello","world","nihao","shijie"};
  • class struct union 可以被看做一个聚合类型
    • 无用户自定义的构造函数
    • 无私有或保护的非静态数据成员(如果存在以上几种情况,就无法使用初始化列表)
#include <iostream>
using namespace std;struct T1
{int x;long y;
protected:int z;
}t{1,100,2};  //error ,类中有保护的成员,无法使用初始化列表初始化struct T2
{int x;long y;
protected:static int z;
}k{1,100,2};  //error 类中有静态成员可以使用初始化列表 ,但初始化列表不能初始化静态成员变量int main(){return 0;
}
  • 无基类
  • 无虚函数

2.非聚合体

  • 对于聚合类型的类可以直接使用列表初始化进行对象的初始化
  • 非聚合体也可以使用列表初始化,需要在类的内部自定义一个构造函数,在构造函数中使用初始化列表对类成员变量进行初始化。

例如:

#include <iostream>
#include <string>
using namespace std;struct Student
{int x;double y;//在构造函数中使用初始化列表初始化类成员Student(int a, double b,int c):x(a),y(b),z(c){}virtual void print(){cout << "x=" << x << " y=" << y << " z=" << z << endl;}private:int z;
};int main(){Student s1{10, 20.5, 30};s1.print();return 0;
}

  •  聚合类型的定义并非递归----简单说  一个类的非静态成员是一个非聚合类型时,这个类也可能是聚合类型。 

例如:

#include <iostream>
#include <string>
using namespace std;struct Student
{int x;double y;//在构造函数中使用初始化列表初始化类成员Student(int a, double b,int c):x(a),y(b),z(c){}Student(){}virtual void print(){cout << "x=" << x << " y=" << y << " z=" << z << endl;}private:int z;
};struct Classroom
{Student s;long x1;double y1;
};
int main(void){Student s1{10, 20.5, 30};s1.print();Classroom c1{{},520,3.14}; //使用{} 对非聚合类进行初始化 ,相当于调用Student的无参构造函数return 0;
}

 三、std::initializer_list

使用

初始化列表只能进行固定函数的初始化,如果想要进行任意长度的初始化 可以使用 initializer_list(轻量级的模版类)

特点:

  • 它是一个轻量级的容器类型,内部定义了迭代器 iterator等容器必须的概念,遍历时得到的迭代器是只读的。
  • std::initializer_list<T> 它可以接收任意长度的初始化列表,但是要求元素必须是同种类型T
  • 在std::initializer_list内部有三个成员接口:size(), begin(), end()

  • 在std::initializer_list 只能被整体初始化或者赋值

1.作为普通函数参数

#include <iostream>
#include <string>
using namespace std;struct Student
{int x;double y;//在构造函数中使用初始化列表初始化类成员Student(int a, double b,int c):x(a),y(b),z(c){}Student(){}virtual void print(){cout << "x=" << x << " y=" << y << " z=" << z << endl;}private:int z;
};struct Classroom
{Student s;long x1;double y1;
};void func(initializer_list<int> ll)
{auto it = ll.begin();for(;it!=ll.end();++it){cout << *it << " ";}cout << endl;
}
int main(void){Student s1{10, 20.5, 30};s1.print();Classroom c1{{},520,3.14};func({1,2,3,4,5,7,8,9,666});return 0;
}

 2.作为构造函数参数

#include <iostream>
#include <string>
#include <vector>
using namespace std;class Test {
public:Test(std::initializer_list<string> list){for (auto it = list.begin(); it != list.end(); ++it){cout << *it << " ";m_names.push_back(*it);}}private:std::vector<string> m_names;
};int main() {Test t({"hello", "world", "c++"});return 0;
}

http://www.dtcms.com/a/273245.html

相关文章:

  • C++ Lambda 表达式详解
  • 《棒垒球知道》奥运会的吉祥物是什么·棒球1号位
  • 【c++八股文】Day6:using和typedef
  • [yolo-world]YOLO-World数据集介绍及标注格式详解
  • SoC程序如何使用单例模式运行
  • 什么是 MIT License?核心要点解析
  • [数据结构与算法] 优先队列 | 最小堆 C++
  • 几种LLM推理加速技术的区别
  • 列表页与详情页的智能识别:多维度判定方法与工业级实现
  • 海光芯赋能:国产化高性能计算平台,重塑边缘与工业智能新算力
  • 使用虚拟机远程登陆ensp模拟器交换机
  • ROS1学习第二弹
  • 1 C++提高——模板
  • H5微应用四端调试工具—网页版:深入解析与使用指南
  • FS-TAS如何提升电催化反应的效率-测试GO
  • 人大金仓下载安装教程总结
  • 区块链基础知识:从比特币到区块链的全面解析
  • 复杂度简介
  • Android-jetpack之DataBinding实战应用
  • NMEA-0183 协议 GPS 介绍
  • Redis-集群Cluster
  • Python练习(1)Python基础类型操作语法实战:20道实战题解与案例分析(上)
  • 《一起出发,“春”不“晚”》特别行动踏梦武当,探寻新春奇境
  • 教育领域AI教师培训计划及相关行业动态的综合简报
  • CVPR2022——RepLKNet模型有效感受野的热图可视化
  • Java Stream流:高效数据处理全解析
  • RV1126平台(Buildroot Linux)+ SunplusIT SPCA2688 USB摄像头 RTSP推流全流程复盘与问题解决记录
  • LabelImg标注工具详解与使用教程
  • SQL进阶:自连接的用法
  • 数字电子时钟——数字电路课设