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;
}
相关推荐
悟能不能悟2 分钟前
JAVA 中dao层的实体应该属于哪个层次VO,还是DTO,或者其他
java·开发语言
二狗哈6 分钟前
Cesium快速入门17:与entity和primitive交互
开发语言·前端·javascript·3d·webgl·cesium·地图可视化
chenyuhao20246 分钟前
Linux系统编程:Ext文件系统
linux·运维·服务器·开发语言·网络·c++·后端
hd51cc1 小时前
MFC运行原理
c++·mfc
沐知全栈开发1 小时前
C 标准库 - <locale.h>
开发语言
老毛肚1 小时前
Java两种代理模式详解
java·开发语言·代理模式
小此方1 小时前
Re:从零开始学C++(二)基础精讲·中篇:引用
开发语言·c++·底层
天赐学c语言1 小时前
12.13 - 岛屿数量 && C语言中extern关键字的作用
c++·算法·leetcode
消失的旧时光-19431 小时前
Java 线程通信:彻底理解 wait / notify(原理 + 图解 + 实战)
java·开发语言
郭涤生2 小时前
大白话Proactor模式
linux·网络·c++