zoukankan      html  css  js  c++  java
  • 组合类的构造函数的调用顺序

    文章来源:https://bbs.csdn.net/topics/390253882

    “构造函数调用顺序:先调用内嵌对象的构造函数(按内嵌时的声明顺序,先声明者先构造)。然后调用本类的构造函数。(析构函数的调用顺序相反)”

    #include<iostream> using namespace std; class Part //部件类 { public: Part() {cout<<"Part default construction called"<<endl;} Part(int i) {cout<<"Part: "<<i<<" construction called"<<endl;} ~Part() {cout<<"Part deconstruction called"<<endl;} private: int val; }; class Whole { public: Whole(); Whole(int i,int j,int k); ~Whole(); private: //这三个变量的声明顺序改变,打印输出的结果仍然一样 Part one; Part two; int date; }; Whole::Whole() { date=0;} Whole::Whole(int i,int j,int k) :one(j),date(k),two(i) //这里的初始化顺序按照one(2),date(3),two(1)进行,按照i,j,k的顺序来的. :/*one(j),*/date(k), two(i) //默认构造one { cout<<"Whole's construction called!"<<endl; } Whole::~Whole() { cout<<"Whole deconstruction called!"<<endl; } int main(void) { Whole w(1, 2, 3); return 1; }

    输出结果:

    Part: 2 construction called
    Part: 1 construction called
    Whole's construction called!


    Whole deconstruction called!
    Part deconstruction called
    Part deconstruction called

    “构造函数调用顺序:先调用内嵌对象的构造函数(按内嵌时的声明顺序,先声明者先构造,比如上面的按照i,j,k)。然后调用本类的构造函数。(析构函数的调用顺序相反)”
    新战场:https://blog.csdn.net/Stephen___Qin
  • 相关阅读:
    Centos系统安装
    HPDL380G8平台11.2.0.3 RAC实施手册
    bash的位置变量和特殊变量
    bash中的算术运算
    bash文本查看及处理工具
    bash快捷键及输入输出重定向
    bash文件名统配
    文本搜索grep知识点总结
    sed练习
    流编辑器sed知识点总结
  • 原文地址:https://www.cnblogs.com/Stephen-Qin/p/11512427.html
Copyright © 2011-2022 走看看