C++构造函数顺序



在这个程序中,当在main()中创建类C的对象时,默认构造函数AX和AXX没有在类C中调用,只有参数构造函数在调用。此处如何省略默认构造函数。。即使我们在类C中创建AX和AXX的对象。正如你在类B中看到的,我没有AX对象的初始化列表,所以默认构造函数在类B内被调用。那么,在这里,我们用来澄清这个概念的基本原理是什么?有人能帮我做下面的节目吗??


#include <iostream>
using namespace std;
class A
{
    public:
    A(){cout << "I am in the A constructor " << endl;}
    ~A(){cout << "I am In the A destructor "<< endl;}
};
class AX
{
    public:
    AX(){cout<<"I am in the AX constructor" << endl;}
    ~AX(){cout <<"I am in the AX destructor" << endl;}
    AX(int x){cout<<"I am in AX param constructor"<< endl;}
};
class AXX
{
    public:
    AXX(){cout << "I amin the AXX constructor"<<endl;}
    ~AXX(){cout << "I am in the AXX destructor "<< endl;}
    AXX(int x)
    {
        cout <<"I am in the AXX param constructor" << endl;
    }
};
 class B : public A
{
     AX ax;
     AXX axx;
    public:
     B():axx(6){cout <<"I amin B constructor"<< endl;}
     ~B(){cout << "I am in  the B destrcuctor "<< endl;}
 };
 class C : public B
 {
     AXX axx;
     AX ax;
     public : 
     C() : ax(5),axx(6) {cout << "I am in c constructor" << endl;}
          ~C(){cout << "I am in the c destructor" << endl;}
 };
int main() {
    // your code goes here
    C c1;
    return 0;
}

实际o/p:

  • 我在A建造师
  • 我在AX构造函数中
  • 我在AXX参数构造函数中
  • 我是B级建造师
  • 我在AXX参数构造函数中
  • 我在AX参数构造函数中
  • 我是c建造师
  • 我在自毁装置里
  • 我在AX析构函数中
  • 我在AXX析构函数中
  • 我在B除菌器里
  • 我在AXX析构函数中
  • 我在AX析构函数中
  • 我在A析构函数中

预期o/p:

  • 我在A建造师
  • 我在AX构造函数中
  • 我在AXX参数构造函数中
  • 我是B级建造师
  • 我在AX构造函数中
  • 我在AXX构造函数中
  • 我在AXX参数构造函数中
  • 我在AX参数构造函数中
  • 我是c建造师
  • 我在自毁装置里
  • 我在AX析构函数中
  • 我在AXX析构函数中
  • 我在B除菌器里
  • 我在AXX析构函数中
  • 我在AX析构函数中
  • 我在A析构函数中

您的期望是错误的。对于每个(子)对象,只调用一个构造函数。因此,对于c1.axx,它将是参数化构造函数,因为c构造函数用一个值初始化它。

您对构建成员的顺序的期望也是错误的。C.axx是在C.ax之前声明的,因此它是首先构造的。ax首先出现在初始值设定项列表中这一事实无关紧要。(有些编译器会警告声明的顺序与初始值设定项列表中的顺序不同。)

该命令的原因是声明命令,即销毁命令与施工命令相反。如果构造顺序是"初始值设定项列表顺序",那么以下类的销毁顺序是什么?(请注意,这两个构造函数在列表中使用不同的顺序)。

class CC
 {
     AXX axx;
     AX ax;
     public : 
     CC() : ax(5),axx(6) {}
     CC(int a, int b) : axx(b), ax(a) {}
 };

在构造派生类之前,每个基类都是完全构造的。之后,顺序由类中的字段顺序定义,每个字段通过构造函数列表具有可选的构造参数。之后,尸体被处决。

要获取您的代码,并展开和排序构造列表,这就是您最终得到的:

#include <iostream>
using namespace std;
class A
{
    public:
    A(){cout << "I am in the A constructor " << endl;}
    ~A(){cout << "I am In the A destructor "<< endl;}
};
class AX
{
    public:
    AX(){cout<<"I am in the AX constructor" << endl;}
    ~AX(){cout <<"I am in the AX destructor" << endl;}
    AX(int x){cout<<"I am in AX param constructor"<< endl;}
};
class AXX
{
    public:
    AXX(){cout << "I amin the AXX constructor"<<endl;}
    ~AXX(){cout << "I am in the AXX destructor "<< endl;}
    AXX(int x)
    {
        cout <<"I am in the AXX param constructor" << endl;
    }
};
 class B : public A
{
     AX ax;
     AXX axx;
    public:
     B() : A(), ax(), axx(6) {cout <<"I amin B constructor"<< endl;}
     ~B(){cout << "I am in  the B destrcuctor "<< endl;}
 };
 class C : public B
 {
     AXX axx;
     AX ax;
     public : 
     C() : B(), axx(6), ax(5) {cout << "I am in c constructor" << endl;}
     ~C(){cout << "I am in the c destructor" << endl;}
 };
int main() {
    // your code goes here
    C c1;
    return 0;
}

发生的事情如下:

  • C的构造函数调用B的构造函数
    • B的构造函数调用A的构造函数
      • A的构造函数调用其主体
    • B的构造函数按源代码顺序调用其字段上的构造函数:
      • ax()的构造函数
      • axx(6)的构造函数
    • B的构造函数调用其主体
  • C的构造函数按源代码顺序调用其字段上的构造函数:
    • axx(6)的构造函数
    • ax(5)的构造函数
  • C的构造函数调用其主体

破坏是相反的顺序。

C中总共有4个字段。即b::axb::axxc::axxc::ax。按顺序,它们由参数AX()AXX(6)AXX(6)AX(5)构成。每个字段都构造正确。

如果不告诉我们你想要实现什么,或者你为什么想要实现,我就无法进一步阐述。代码正在按预期工作。

最新更新