类构造函数的顺序

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; font-family:Arial; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; layout-grid-mode:line;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

#include <iostream>

using namespace std;

class BaseMember

{

   public:

      BaseMember()

      {

         cout<<" BaseMember construct "<<endl;

       }

};

class Base

{

   BaseMember m_member;

   public:

     Base()

     {

        cout<<" Base construct "<<endl;

      }

};

class Member

{

    public:

       Member( const char* msg)

       {

           cout<<" Member construct; "<<msg<<endl;

        }

};

class Subclass:public Bsae

{

    Member m_memberB;

    Member m_memberA;

    public:

      Subclass(): m_memberA ("MemberA"), m_memberB ("MemberB")

      {

          cout<<" Subclass construct "<<endl;

       }

};

void TestConstruct()

{

    Subclass sub;

}

TestConstruct() 函数的运行结果是:

 

 

BaseMember construct

 Base construct

 Member construct; MemberB

 Member construct; MemberA

 Subclass construct



例2:

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; font-family:Arial; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; layout-grid-mode:line;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1740012694; mso-list-type:hybrid; mso-list-template-ids:24533836 -1323023228 -1395488594 -1130312782 -277318296 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1、; mso-level-tab-stop:18.0pt; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} @list l0:level2 {mso-level-text:%2、; mso-level-tab-stop:39.0pt; mso-level-number-position:left; margin-left:39.0pt; text-indent:-18.0pt;} @list l0:level3 {mso-level-text:"/(%3/)"; mso-level-tab-stop:60.75pt; mso-level-number-position:left; margin-left:60.75pt; text-indent:-18.75pt;} @list l0:level4 {mso-level-number-format:alpha-lower; mso-level-text:%4、; mso-level-tab-stop:81.0pt; mso-level-number-position:left; margin-left:81.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

1、 构造函数调用顺序。

class Y {...}
class X : public Y {...}
X one;

构造函数的调用顺序是下面的顺序:

Y(); // 基类的构造函数
X(); //
继承类的构造函数

对于多基类的情况,下面是一个例子:

class X : public Y, public Z
X one;

构造函数以声明的次序调用。

Y(); // 基类构造函数首先被调用
Z();
X();

虚基类的构造函数在任何非虚基类构造函数前调用。如果构造中包括多个虚基类,它们的调用顺序以声明顺序为准。如果虚类是由非虚类派生而来,那非虚类的构造函数要先被调用。下面是一个例子:

class X : public Y, virtual public Z
X one;

调用顺序如下:

Z(); // 虚基类初始化
Y(); //
非虚基类
X(); //
继承类

下面是一个复杂的例子:

class base;
class base2;
class level1 : public base2, virtual public base;
class level2 : public base2, virtual public base;
class toplevel : public level1, virtual public level2;
toplevel view;

构造函数调用顺序如下:

base(); // 虚基类仅被构造一次
base2();
level2(); //
虚基类
base2();
level1();
toplevel();

从结果我们可以总结出:

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; font-family:Arial; mso-fareast-font-family:宋体; mso-bidi-font-family:"Times New Roman"; layout-grid-mode:line;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

如果类继承中包括多个虚基类的实例,基类只被初始化一次。

1 、如果类里面有成员类,成员类的构造函数优先被调用;

2 、创建派生类的对象,基类的构造函数函数优先被调用(也优先于派生类里的成员类);

3 基类构造函数如果有多个基类则构造函数的调用顺序是某类在类派生表中出现的
顺序而不是它们在成员初始化表中的顺序;
4
、成员类对象构造函数如果有多个成员类对象则构造函数的调用顺序是对象在类中
被声明的顺序而不是它们出现在成员初始化表中的顺序;
5
、派生类构造函数
作为一般规则派生类构造函数应该不能直接向一个基类数据成员赋值而是把值传递
给适当的基类构造函数否则两个类的实现变成紧耦合的( tightly coupled )将更加难于
正确地修改或扩展基类的实现。(基类设计者的责任是提供一组适当的基类构造函数)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值