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;
}
相关推荐
卷无止境1 天前
C++ 的Eigen 库全解析
c++
卷无止境1 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴1 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18003 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴3 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨4 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint4568 天前
C++进阶(1)——前景提要
c++
夜悊8 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴8 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0019 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp