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;
}
相关推荐
weixin_440730506 小时前
函数return、参数、参数组、递归函数、高阶函数等小结
开发语言·python
sugarzhangnotes6 小时前
【无标题】
开发语言·人工智能·python
geovindu6 小时前
rust: Composite Pattern
开发语言·后端·设计模式·rust·组合模式
xailorliu7 小时前
SLIC_超像素算法
c++·opencv·目标检测·机器学习·计算机视觉
苏打豆7 小时前
CRISP源码阅读——基于ROS2力矩反馈控制器与速度加速度零空间
c++·线性代数·算法·矩阵·机器人
周杰偷奶茶7 小时前
【Java】数组的定义和使用(附管理系统实战案例)
java·开发语言
小小龙学IT8 小时前
snap7 开源西门子 S7 通信协议库深度解析
c++·开源
橙子圆1238 小时前
JUC之集合类不安全
java·开发语言
阿里嘎多学长8 小时前
2026-09-29 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Bruce_Liuxiaowei8 小时前
网页版 DeepSeek “harness desktop“ WebGL 上下文创建失败排查实录
开发语言·javascript·webgl