C++ —— 常见的初始化

目录

一,默认初始化(Default Initialization)

二,直接初始化(Direct Initialization)

三,拷贝初始化(Copy Initialization)

四,值初始化(Value Initialization)

五,列表初始化(List Initialization)

六,成员初始化列表(Member Initialization List)

七,显式初始化(Explicit Initialization)

八,静态初始化(Static Initialization)

九,引用初始化(Reference Initialization)

十,聚合初始化(Aggregate Initialization)

十一,延迟初始化(Lazy Initialization)


参考:

C++中常见的初始化类型_c++初始化类-CSDN博客

https://en.cppreference.com/w/cpp/language/initialization

        变量的初始化是在构造时提供其初始值,初始值可以在声明或新表达式的初始化部分提供;初始化也发生在函数调用期间:函数参数和函数返回值也被初始化;

#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
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 };
( other )

( arg1, arg2, ... )

static_cast< T >( other )
new T( args, ... )
Class::Class() : member( args, ... ) { ... }
[arg]() { ... }lambda表达式(C++11)
int main() {
    MyClass obj(10);  //调用 MyClass(int) 有参构造函数 
    return 0;
}

三,拷贝初始化(Copy Initialization)

拷贝初始化是另一个对象初始化该对象,调用拷贝构造函数或移动构造函数;

Syntax

T object = other;
T object = {other};{}拷贝初始化对象(C++11)
f(other)值传参
return other;值返回
throw object;

catch (T object)

值抛出和捕获
T array[N] = {other-sequence};初始化每个元素
int main() {
    MyClass obj1(10);  // 调用 MyClass(int) 构造函数
    MyClass obj2 = obj1;  // 调用拷贝构造函数 
    return 0;
}

四,值初始化(Value Initialization)

值初始化使用空值构造对象时执行的初始化;

  • 对于类类型,会调用其默认构造函数;

Syntax

()匿名临时对象
new T ()动态构造对象
Class::Class(...) : member () { ... }使用成员初始化对象
T object {};{}初始化对象(C++11)
{}{}匿名临时对象(C++11)
new T {}{}动态构造对象(C++11)
Class::Class(...) : member {} { ... }{}使用成员初始化对象(C++11)
int main() {
    int a = int();  //初始化为 0
    MyClass obj = MyClass();  //匿名对象,调用 MyClass 的默认构造函数
    return 0;
}

五,列表初始化(List Initialization)

列表初始化使用大括号 `{}` 进行初始化;

  • C++11引入,可以避免窄化转换,并支持统一的初始化方式;

Syntax

Direct-list-initialization

T object { arg1, arg2, ... };
{ 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, ... }
({ arg1, arg2, ... })
Class { T member = { arg1, arg2, ... }; };
int main() {
    int a{5};         //初始化为 5
    MyClass obj{10};  //调用 MyClass(int) 有参构造函数    
    return 0;
}

六,成员初始化列表(Member Initialization List)

用于在类的构造函数中初始化成员变量;

#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)

使用显式构造函数进行初始化,防止隐式转换;

#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 = {} ;

{} ; (C++11)

CharT array [ n ] = " short-sequence ";
#include <iostream>
 
class MyClass {
public:
    static int count;
    static const int constant;
};
 
int MyClass::count = 0;  //静态初始化
const int MyClass::constant = 42;  //常量初始化

九,引用初始化(Reference Initialization)

引用变量必须在声明时进行初始化,且引用类型的初始化方式与其引用对象一致;

#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)
#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)

延迟初始化是指在需要时才进行初始化,常用于优化性能,减少资源占用;

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值