C++ C++;构造命令

C++ C++;构造命令,c++,C++,在这个程序中,当在main()中创建类C的对象时,默认构造函数AX和AXX没有在类C中调用,只有参数构造函数正在调用。此处如何省略默认构造函数。即使我们在类C内创建AX和AXX的对象。正如您在类B中看到的,我没有AX对象的初始化列表,因此默认构造函数在类B内被调用。 那么,我们在这里用什么来澄清这个概念呢? 有人能帮我完成以下项目吗 #包括 使用名称空间std; 甲级 { 公众: A(){cout您的期望是错误的。对于每个(子)对象,只调用一个构造函数。因此对于c1.axx,它将是参数化构造函

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


#包括
使用名称空间std;
甲级
{
公众:

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

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

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


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

要获取代码,并展开和排序构造列表,您将得到以下结果:

#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;
}
#包括
使用名称空间std;
甲级
{
公众:

(){cout一些更具体的问题描述会非常有帮助。我看到所有的构造函数都被调用:参数构造函数在C类中是如何调用的,但在此之前我创建了AX和AXX的对象..因此,由于这个默认构造函数应该被正确调用..但它没有调用,而是直接调用参数AX和AXX的r构造函数…@CadInitialization列表在构造函数主体之前进行处理。这是一件好事。@JitendraMahari请发表文章,而不是将这些注释添加到C类的输出端。我们在C构造函数之前有AXX AXX;AX AX;。那么为什么不在参数之前调用这些默认构造函数呢构造器@Aidiakapi@JitendraMahari因为正如我所提到的,无论您在源代码中键入什么,构造顺序都是固定的。它首先构造基类,然后按照它们在类体中定义的顺序构造所有字段。而不是按照它们在构造函数列表中定义的顺序。@JitendraMahari这是also为什么它被认为是一个bug,或者当构造函数列出了与字段顺序不同的代码气味时。像RESHARPEC++的工具会告诉你对构造函数列表进行排序以匹配字段布局。,即使我们已经为该类创建了一个对象。我说的对吗@Aidiakapi@JitendraMahari不,我要说的是,如果你有
类N:public M{type fX,fY;N(type fY,type fX):fY(fY),fX(fX),M(fX,fY){};
它将按照
:M(fX,fY),fX(fX),fY(fY)的顺序执行
。因为它首先构造基类,然后按照在类主体中定义字段的顺序构造字段。构造函数列表的顺序(由于C++11s初始化器列表,我更喜欢术语初始化器列表)根本不重要。顺序与您在初始化器列表中键入的内容无关。
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;
}