惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
V
Visual Studio Blog
The Register - Security
The Register - Security
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
大猫的无限游戏
大猫的无限游戏
L
LangChain Blog
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
T
Threatpost
P
Proofpoint News Feed
美团技术团队
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog
Vercel News
Vercel News
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
S
Security @ Cisco Blogs
AWS News Blog
AWS News Blog
SecWiki News
SecWiki News
I
InfoQ
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
B
Blog RSS Feed
H
Hacker News: Front Page
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary

博客园 - 新西兰程序员

C++中的线程thread,以及C++11中的std::atomic学习 Leetcode56 Merge Intervals 合并区间 -- C++实现 C++中的内存对齐 C++中的static关键字使用以及全局变量 C++17中的结构化绑定Structured Binding C++中的函数参数是指针类型, 是按值传递 C++中异常处理机制中的栈展开stack unwinding C++实现链表反转 Linux中的性能分析工具 perf 来分析C++性能 C++17中新建一个类时,编译器默认生成的类成员函数 QT中的元对象系统 Leetcode 114 - 二叉树展开为链表 Leetcode65 有效数字 - 判断给定的字符串是否是一个有效数字 LettCode2289-Steps to Make Array Non-descreasing C#中的委托详解 C++中的std::function C++中的仿函数Functor C++中的const和constexpr异同比较 C++中的左值和右值,以及右值引用,移动语义 C++中传递参数是指针类型以及传入参数是指针的指针(**)详解 C++中GetTickCount函数学习 C++中以类的成员函数作为Windows callback函数需要设置成static函数 C#中的System.Security.SecureString学习 C++中的悬挂指针和野指针 C++中四种不同的对象生存方式(in stack, in heap, global, local static) C#中使用Parallel类来进行多线程并发编程 String类型转LPCTSTR -----理解C++中的字符串类型转换 C++中的虚函数和虚函数表 C++中基类指针指向派生类对象 数据结构 - 栈的学习
C++ 构造函数中的初始化参数列表initializer list
新西兰程序员 · 2026-07-01 · via 博客园 - 新西兰程序员

今天我们来说说C++中的构造函数初始列表initializer list, 我们来看一个例子

class Task
{
private:
    int id;
public:
    Task(int id) :id(id)
    {
        std::cout << "生产:" << id << std::endl;
    }
}

 看到这句 可能会有疑惑:

Task(int id) : id(id)  => C#中是不能这么写的,C++里这样写是什么意思? 它在C++中叫做  构造函数初始化列表,分拆如下

含义分拆如下

部分意义
Task(int id) 构造函数参数
: 表示接下来是初始化列表
id(id) 用参数 id 初始化成员变量 id

也就是说  =>  “把传进来的 id,用来初始化类里的 id 成员变量”   

C++中为何可以这样写呢?  =》 C++规定,成员变量在进入函数构造体之前必须先完成初始化

 上面那个例子,有人会这样写

class Task
{
  private:
    int id;
  public:
    Task(int id)
    {
        this -> id = id;
        std::cout << "生产:" << id << std::endl;
    }
}

这样写和上面那样构造函数初始化列表有什么区别呢 => 它们看起来类似,但本质上差别很大

方式行为
: id(id) ✅ 直接初始化(推荐)
this->id = id ❌ 先默认初始化,再赋值

 我们来看这个例子进行比较

class A {
              int x;
              public:
                    A(int val) : x(val) {}        // ✅ 直接构造
                    A(int val) { x = val; }       // ❌ 先构造再赋值
};

上面类A中的第二个构造函数,就相当于

x = 0; // 默认初始化

x= val; // 把x默认初始化为0后,再把变量val的值赋给它

 必须使用构造函数初始化列表的情况

 1. const成员

class A {
    const int x;
public:
    A(int val) : x(val) {}   // ✅ 必须这样写
};

你不能这样写

class A {
    const int x;
public:
    A(int val) 
    {
         x = val; //不能这样写,因为x是const变量,不能赋值
     }  
};

2. 引用成员

class A {
    int& ref;
public:
    A(int& r) : ref(r) {}   // ✅ 必须初始化
};

3. 成员是对象

class B {
public:
    B(int x) {}
};

class A {
    B b;
public:
    A() : b(10) {}  // ✅ 必须这样写
};

C++中的构造函数初始化列表有不少好处:

  • ✅ 性能更好(少一次赋值)
  • ✅ 代码更清晰
  • ✅ 避免未初始化风险
  • ✅ 必要场景统一风格

我们再来看一下  =》 初始化按变量声明顺序

class A 
{
    int x;
    int y;
    public:
           A(int a, int b) : y(b), x(a) {}
};

在这个例子中,注意它是x先初始化,y后面再初始化. 因为初始化按变量声明顺序,而不是按照初始化列表顺序