CodeRoadMap
路线图学习路径文章题库资源社区

浏览

首页路线图学习路径知识库题库文章资源社区我的学习
CodeRoadMap

中文编程学习导航:路线图、讲义与题库,进度可同步。

路线图学习路径文章题库社区

© 2026 CodeRoadMap

津ICP备2026012044号-1|coderoadmap@126.com
首页/C++ 那些事/初始化列表与赋值

课程目录

  1. 01const 那些事
  2. 02static 那些事
  3. 03this 那些事
  4. 04inline 那些事
  5. 05sizeof 那些事
  6. 06纯虚函数和抽象类那些事
  7. 07vptr_vtable 那些事
  8. 08virtual 那些事
  9. 09volatile 那些事
  10. 10assert 那些事
  11. 11位域那些事
  12. 12extern 那些事
  13. 13struct 那些事
  14. 14struct 与 class 那些事
  15. 15union 那些事
  16. 16用 C 实现 C++ 多态
  17. 17explicit 那些事
  18. 18friend 那些事
  19. 19using 那些事
  20. 20:: 那些事
  21. 21enum 那些事
  22. 22decltype 那些事
  23. 23引用与指针那些事
  24. 24宏那些事
  25. 25C++11 新特性
  26. 26C++ STL源码剖析 tr1与std array
  27. 27C++ STL源码剖析之序列式容器deque
  28. 28C++ STL源码剖析之哈希表
  29. 29C++ STL源码剖析之实现一个简单的iterator_category
  30. 30C++ STL源码剖析之双向环形链表list
  31. 31C++ STL源码剖析之map、multimap、initializer_list
  32. 32从0到1打牢算法基础之手写一个哈希表
  33. 33C++ STL源码剖析之容器配接器stack与queue、priority_queue
  34. 34C++ STL源码剖析之红黑树
  35. 35STL之set与multiset那些事
  36. 36C++ STL 源码剖析之 Traits 编程技法
  37. 37typename
  38. 38C++ STL源码剖析之unordered_map、unordered_multimap、unordered_set、unordered_multiset
  39. 39STL源码剖析之vector
  40. 40STL设计之EBO(空基类优化)
  41. 41设计模式:单例模式
  42. 42初始化列表与赋值
  43. 43C++惯用法之enum class
  44. 44C++惯用法之消除垃圾收集器-资源获取即初始化方法(RAII)
  45. 45copy-swap 惯用法
  46. 46C++惯用法之pImpl
  47. 4710日c++实战狂练
  48. 48Day4 · const 练习
  49. 49��Ԫ���
  50. 50Day5 · 构造与析构
  51. 51Day5 · 继承与访问控制
  52. 52Day5 · 运算符重载
  53. 53Day5 · 三五法则
  54. 54Day5 · virtual 练习
  55. 55Day6 · 综合练习
  56. 56Day7 · 综合练习
  57. 57Day8 · 综合练习
  58. 58异常处理
  59. 59Day10 · 综合练习
  60. 60重点实战练习
第 42 课40 分钟

初始化列表与赋值

- const成员的初始化只能在构造函数初始化列表中进行

课程内容

初始化列表与赋值

  • const成员的初始化只能在构造函数初始化列表中进行
  • 引用成员的初始化也只能在构造函数初始化列表中进行
  • 对象成员(对象成员所对应的类没有默认构造函数)的初始化,也只能在构造函数初始化列表中进行

类之间嵌套

第一种: 使用初始化列表。

class Animal {
public:
    Animal() {
        std::cout << "Animal() is called" << std::endl;
    }

    Animal(const Animal &) {
        std::cout << "Animal (const Animal &) is called" << std::endl;
    }

    Animal &operator=(const Animal &) {
        std::cout << "Animal & operator=(const Animal &) is called" << std::endl;
        return *this;
    }

    ~Animal() {
        std::cout << "~Animal() is called" << std::endl;
    }
};

class Dog {
public:
    Dog(const Animal &animal) : __animal(animal) {
        std::cout << "Dog(const Animal &animal) is called" << std::endl;
    }

    ~Dog() {
        std::cout << "~Dog() is called" << std::endl;
    }

private:
    Animal __animal;
};

int main() {
    Animal animal;
    std::cout << std::endl;
    Dog d(animal);
    std::cout << std::endl;
    return 0;
}

运行结果:

Animal() is called

Animal (const Animal &) is called
Dog(const Animal &animal) is called

~Dog() is called
~Animal() is called
~Animal() is called

依次分析从上到下:

main函数中Animal animal;调用默认构造。

Dog d(animal);等价于:

Animal __animal = animal;

实际上就是调用了拷贝构造,因此输出了:

Animal (const Animal &) is called

再然后打印Dog的构造函数里面的输出。

最后调用析构,程序结束。

第二种:构造函数赋值来初始化对象。

构造函数修改如下:

Dog(const Animal &animal) {
    __animal = animal;
    std::cout << "Dog(const Animal &animal) is called" << std::endl;
}

此时输出结果:

Animal() is called

Animal() is called
Animal & operator=(const Animal &) is called
Dog(const Animal &animal) is called

~Dog() is called
~Animal() is called
~Animal() is called

于是得出:

当调用Dog d(animal);时,等价于:

先定义对象,再进行赋值,因此先调用了默认构造,再调用=操作符重载函数。

// 假设之前已经有了animal对象
Animal __animal;
__animal = animal;

小结

通过上述我们得出如下结论:

  • 类中包含其他自定义的class或者struct,采用初始化列表,实际上就是创建对象同时并初始化
  • 而采用类中赋值方式,等价于先定义对象,再进行赋值,一般会先调用默认构造,在调用=操作符重载函数。

无默认构造函数的继承关系中

现考虑把上述的关系改为继承,并修改Animal与Dog的构造函数,如下代码:

class Animal {
public:
    Animal(int age) {
        std::cout << "Animal(int age) is called" << std::endl;
    }

    Animal(const Animal & animal) {
        std::cout << "Animal (const Animal &) is called" << std::endl;
    }

    Animal &operator=(const Animal & amimal) {
        std::cout << "Animal & operator=(const Animal &) is called" << std::endl;
        return *this;
    }

    ~Animal() {
        std::cout << "~Animal() is called" << std::endl;
    }
};

class Dog : Animal {
public:
    Dog(int age) : Animal(age) {
        std::cout << "Dog(int age) is called" << std::endl;
    }

    ~Dog() {
        std::cout << "~Dog() is called" << std::endl;
    }

};

上述是通过初始化列表给基类带参构造传递参数,如果不通过初始化列表传递,会发生什么影响?

去掉初始化列表

Dog(int age)  {
    std::cout << "Dog(int age) is called" << std::endl;
}

运行程序:

error: no matching function for call to ‘Animal::Animal()’

由于在Animal中没有默认构造函数,所以报错,遇到这种问题属于灾难性的,我们应该尽量避免,可以通过初始化列表给基类的构造初始化。

类中const数据成员、引用数据成员

特别是引用数据成员,必须用初始化列表初始化,而不能通过赋值初始化!

例如:在上述的Animal中添加私有成员,并修改构造函数:

class Animal {
public:
    Animal(int age,std::string name) {
        std::cout << "Animal(int age) is called" << std::endl;
    }
private:
    int &age_;
    const std::string name_;
};

报下面错误:

error: uninitialized reference member in ‘int&’

应该改为下面:

Animal(int age, std::string name) : age_(age), name_(name) {
    std::cout << "Animal(int age) is called" << std::endl;
}
← 上一课设计模式:单例模式下一课 →C++惯用法之enum class