C++ 27 之 初始化列表

c27初始化列表.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
using namespace std;

class Students06{
public:
    int s_a;
    int s_b;
    int s_c;
    Students06(int a, int b, int c){
        s_a = a;
        s_b = b;
        s_c = c;
    }

    // 初始化列表写法1:
    // Students06():s_a(4),s_b(5),s_c(6)
    // {

    // }
    // 初始化列表写法2:
    // Students06(int a, int b, int c) : s_a(a), s_b(b), s_c(c)
    // {

    // }
};


int main()
{
    // 普通写法
    Students06 stu1(1, 2, 3);
    cout << stu1.s_a << endl;
    cout << stu1.s_b << endl;
    cout << stu1.s_c << endl;

    // 初始化列表写法1:
    // Students06 stu1;
    // cout << stu1.s_a << endl;
    // cout << stu1.s_b << endl;
    // cout << stu1.s_c << endl;

    // 初始化列表写法2: 
    // Students06 stu1(7,8,9);
    // cout << stu1.s_a << endl;
    // cout << stu1.s_b << endl;
    // cout << stu1.s_c << endl;


    return 0;
}
相关推荐
坚果派·白晓明3 小时前
【鸿蒙PC三方库移植适配框架解读系列】第八篇:扩展lycium框架使其满足rust三方库适配
c语言·开发语言·华为·rust·harmonyos·鸿蒙
花间相见3 小时前
【PaddleOCR教程01】PP-OCRv5 全面指南:从模型架构到实战部署
开发语言·r语言
小短腿的代码世界4 小时前
Qt 股票订单撮合引擎:高频交易系统的核心心脏
开发语言·数据库·qt·系统架构·交互
REDcker4 小时前
C++变量存储与ELF段布局详解 从const全局到rodata与nm_readelf验证实践
java·c++·面试
谙弆悕博士5 小时前
快速学C语言——第16章:预处理
c语言·开发语言·chrome·笔记·创业创新·预处理·业界资讯
yuan199976 小时前
基于 C# 实现的 Omron HostLink (FINS) 协议 PLC 通讯
开发语言·c#
王老师青少年编程6 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:合并序列
c++·字符串·csp·高频考点·信奥赛·字符串排序·合并序列
qq_422828626 小时前
android图形学之SurfaceControl和Surface的关系 五
android·开发语言·python
handler017 小时前
UDP协议与网络通信知识点
c语言·网络·c++·笔记·网络协议·udp
如竟没有火炬7 小时前
用队列实现栈
开发语言·数据结构·python·算法·leetcode·深度优先