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
  • 相关阅读:
    C# 生成随机索引列表
    QQ音乐MP3下载
    微信Dat文件解码
    C#工作常用关键字
    C#左移运算符
    C#中datatable操作
    html 显示 pdf
    framework7 下拉刷新、无限滚动
    framework7 总结之前遇到的问题和踩过的坑
    HTML 引用大全
  • 原文地址:https://www.cnblogs.com/Stephen-Qin/p/11512427.html
Copyright © 2011-2022 走看看