目录
[一,默认初始化(Default Initialization)](#一,默认初始化(Default Initialization))
[二,直接初始化(Direct Initialization)](#二,直接初始化(Direct Initialization))
[三,拷贝初始化(Copy Initialization)](#三,拷贝初始化(Copy Initialization))
[四,值初始化(Value Initialization)](#四,值初始化(Value Initialization))
[五,列表初始化(List Initialization)](#五,列表初始化(List Initialization))
[六,成员初始化列表(Member Initialization List)](#六,成员初始化列表(Member Initialization List))
[七,显式初始化(Explicit Initialization)](#七,显式初始化(Explicit Initialization))
[八,静态初始化(Static Initialization)](#八,静态初始化(Static Initialization))
[九,引用初始化(Reference Initialization)](#九,引用初始化(Reference Initialization))
[十,聚合初始化(Aggregate Initialization)](#十,聚合初始化(Aggregate Initialization))
[十一,延迟初始化(Lazy Initialization)](#十一,延迟初始化(Lazy Initialization))
参考:
变量的初始化是在构造时提供其初始值,初始值可以在声明或新表达式的初始化部分提供;初始化也发生在函数调用期间:函数参数和函数返回值也被初始化;
cpp#include <iostream> //类类型 class MyClass { public: //无参默认构造函数 MyClass() : x(0) { std::cout << "MyClass() default constructor" << std::endl; } //有参构造函数 MyClass(int val) : x(val) { std::cout << "MyClass(int val) default constructor" << std::endl; } //拷贝构造函数 MyClass(const MyClass& other) : x(other.x) { std::cout << "MyClass(const MyClass& other) copy constructor" << std::endl; } int x; };
一,默认初始化(Default Initialization)
默认初始化是指在声明变量时未提供初始值;
- 内置类型变量,值未定义;
- 类类型变量,调用其默认构造函数;
|--------------|
| Syntax |
| T object ; |
| new T |
cpp
int main() {
int a; //内置类型,值未定义
MyClass obj; //类型类型,调用 MyClass 的无参默认构造函数,输出 0
new MyClass;
return 0;
}
二,直接初始化(Direct Initialization)
直接初始化使用显示构造函数初始化对象;
|--------------------------------------------------------------------------|------------------|
| Syntax ||
| T object ( arg ); T object ( arg1, arg2, ... ); | |
| T object { arg }; | |
| T ( other ) T ( arg1, arg2, ... ) | |
| static_cast< T >( other ) | |
| new T**(** args, ... ) | |
| Class**::Class()** : member**(** args, ... ) { ... } | |
| arg { ... } | lambda表达式(C++11) |
cpp
int main() {
MyClass obj(10); //调用 MyClass(int) 有参构造函数
return 0;
}
三,拷贝初始化(Copy Initialization)
拷贝初始化是另一个对象初始化该对象,调用拷贝构造函数或移动构造函数;
|-----------------------------------------------|------------------------|
| Syntax ||
| T object = other**;** | |
| T object = {other}; | {}拷贝初始化对象
(C++11) |
| f**(other)** | 值传参 |
| return other**;** | 值返回 |
| throwobject**;** catch (T object) | 值抛出和捕获 |
| T array**[N] = {other-sequence};** | 初始化每个元素 |
cpp
int main() {
MyClass obj1(10); // 调用 MyClass(int) 构造函数
MyClass obj2 = obj1; // 调用拷贝构造函数
return 0;
}
四,值初始化(Value Initialization)
值初始化使用空值构造对象时执行的初始化;
- 对于类类型,会调用其默认构造函数;
|-------------------------------------------------------------------|------------------------|
| Syntax ||
| T () | 匿名临时对象 |
| new T () | 动态构造对象 |
| Class**::Class(...)** : member () { ... } | 使用成员初始化对象 |
| T object {}; | {}初始化对象
(C++11) |
| T {} | {}匿名临时对象(C++11) |
| new T {} | {}动态构造对象(C++11) |
| Class::Class(...) : member {} { ... } | **{}**使用成员初始化对象(C++11) |
cpp
int main() {
int a = int(); //初始化为 0
MyClass obj = MyClass(); //匿名对象,调用 MyClass 的默认构造函数
return 0;
}
五,列表初始化(List Initialization)
列表初始化使用大括号 `{}` 进行初始化;
- C++11引入,可以避免窄化转换,并支持统一的初始化方式;
|------------------------------------------------------------------|---|
| Syntax ||
| Direct-list-initialization ||
| T object { arg1, arg2, ... }; | |
| T { arg1, arg2, ... } | |
| new T { arg1, arg2, ... } | |
| Class { T member { arg1, arg2, ... }; }; | |
| Class**::Class() :** member { arg1, arg2, ... } {... | |
| Copy-list-initialization ||
| T object = { arg1, arg2, ... }; | |
| function ({ arg1, arg2, ... }) | |
| return { arg1, arg2, ... }; | |
| object [{ arg1, arg2, ... }] | |
| object = { arg1, arg2, ... } | |
| U ({ arg1, arg2, ... }) | |
| Class { T member = { arg1, arg2, ... }; }; | |
cpp
int main() {
int a{5}; //初始化为 5
MyClass obj{10}; //调用 MyClass(int) 有参构造函数
return 0;
}
六,成员初始化列表(Member Initialization List)
用于在类的构造函数中初始化成员变量;
cpp
#include <iostream>
class MyClass {
public:
MyClass(int a, int b)
: x(a), y(b)
{ std::cout << "MyClass(int, int) constructor" << std::endl; }
int x;
int y;
};
int main() {
MyClass obj(10, 20); //使用成员初始化列表
return 0;
}
七,显式初始化(Explicit Initialization)
使用显式构造函数进行初始化,防止隐式转换;
cpp
#include <iostream>
class MyClass {
public:
explicit MyClass(int val)
: x(val)
{ std::cout << "explicit MyClass(int) constructor" << std::endl; }
int x;
};
int main() {
MyClass obj1(10); //直接初始化
// MyClass obj2 = 10; // 错误:不能进行隐式转换
return 0;
}
八,静态初始化(Static Initialization)
静态变量的初始化
- 常量初始化(Constant initialization),将静态变量的初始值设置为编译时常数;
- 零初始化(Zero initialization),设置对象的初始值为0;
|--------------------------------------------------------------|---|
| 零初始化 Syntax ||
| static T object ; | |
| T () ; T t = {} ; T {} ; (C++11) | |
| CharT array [ n ] = " short-sequence "; | |
cpp
#include <iostream>
class MyClass {
public:
static int count;
static const int constant;
};
int MyClass::count = 0; //静态初始化
const int MyClass::constant = 42; //常量初始化
九,引用初始化(Reference Initialization)
引用变量必须在声明时进行初始化,且引用类型的初始化方式与其引用对象一致;
cpp
#include <iostream>
int main() {
int x = 10;
int& ref = x; //引用初始化
return 0;
}
十,聚合初始化(Aggregate Initialization)
用于聚合类型(如数组和 `struct `),直接使用大括号 `{}` 为成员变量赋值;
|---------------------------------------------------------------------------------|---------|
| Syntax ||
| T object
= { arg1, arg2, ... }; | |
| T object { arg1, arg2, ... }; | (C++11) |
| T object
= { .des1
=
arg1 **, .**des2 { arg2 } ... }; | (C++20) |
| T object { .des1
=
arg1 **, .**des2 { arg2 } ... }; | (C++20) |
cpp
#include <iostream>
struct MyStruct {
int a;
float b;
};
int main() {
MyStruct s1 = {1, 2.5f}; //聚合初始化
int arr[3] = {1, 2, 3}; //数组的聚合初始化
return 0;
}
十一,延迟初始化(Lazy Initialization)
延迟初始化是指在需要时才进行初始化,常用于优化性能,减少资源占用;
cpp
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() {
{ std::cout << "MyClass() default constructor" << std::endl; }
void doSomething()
{ std::cout << "Doing something" << std::endl; }
};
int main() {
std::unique_ptr<MyClass> ptr; // 声明但不初始化
// ...
if (!ptr) {
ptr = std::make_unique<MyClass>(); // 在需要时初始化
}
ptr->doSomething();
return 0;
}