Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:在初始值设定项列表中,更新类成员并将其传递给基类构造函数(在调用它之前)的顺序是什么?_C++_Oop_Inheritance_Constructor_Initializer List - Fatal编程技术网

C++ C++;:在初始值设定项列表中,更新类成员并将其传递给基类构造函数(在调用它之前)的顺序是什么?

C++ C++;:在初始值设定项列表中,更新类成员并将其传递给基类构造函数(在调用它之前)的顺序是什么?,c++,oop,inheritance,constructor,initializer-list,C++,Oop,Inheritance,Constructor,Initializer List,在我的代码中,有两个类: Vect3表示三维向量,即x,y&z 平面表示平面方程中的系数,即a,b,c,&d 现在,Vect3是我的基类,Plane是派生类 Vect3class(基类): 但是,对于以下main int main() { std::vector<float> vec1 = {1.0, 1.0, 1.0, 1.0}; Plane plane1(vec1); plane1.printVect3Magnitude(); plane1.

在我的代码中,有两个类:

  • Vect3
    表示三维向量,即
    x
    y
    &
    z
  • 平面
    表示平面方程中的系数,即
    a
    b
    c
    ,&
    d
  • 现在,
    Vect3
    是我的基类,
    Plane
    是派生类

    • Vect3
      class(基类):
    但是,对于以下
    main

    int main() {
        std::vector<float> vec1 = {1.0, 1.0, 1.0, 1.0};
    
        Plane plane1(vec1);
        plane1.printVect3Magnitude();
        plane1.printAngleWrtAxisDeg();
    
        std::cout << "= = = = = = = = = = = =\n";
    
        Vect3 vect3d(vec1);
        vect3d.printVect3Magnitude();
        vect3d.printAngleWrtAxisDeg();
    
        return 0;
    }
    
    平面
    构造函数中,使用初始值设定项列表,我已经在更新
    \u a
    \u b
    ,&
    \u c
    构造函数,然后将它们传递给
    Vect3
    构造函数。 因此,理想情况下,对象
    平面
    和对象
    vect3d
    printAngleWrtAxisDeg()
    方法的输出应该相同

    但是,根据上述输出,垃圾值似乎正在传递给
    Vect3
    constructor


    另一方面,如果我修改
    平面
    构造函数如下(即,不传递
    \u a
    \u b
    \u c
    向量3
    构造函数,直接传递输入向量
    平面系数


    但是为什么呢?在初始化列表中,类成员被更新并传递给基类构造函数(在被调用之前)的顺序是否有误?

    您注意到编译器警告了吗? 无论在构造函数初始值设定项列表中如何排列成员和基类,它们都将按声明顺序构造,并按相反的声明顺序进行销毁。由于基列表在嵌套所有成员的块之前声明,因此在构造时,所有基子对象都位于成员之前。如果-在构造函数初始值设定项列表中-未放置成员和/或基的初始值设定项,则会生成诊断警告,但代码会编译。如果一个成员或基被初始化为在其之后声明的另一个基/成员的值,则会产生UB——正如OP已经观察到的那样

    最好的,
    FM.

    是的,是的,因此您不能从派生成员初始化基。请为您的编译器设置警告级别。然后它可能会告诉您,成员将不会按照您列出的顺序进行初始化。@alain,感谢您共享该问题链接。因此,基本上,无论我以何种顺序初始化成员变量,基类构造函数总是首先被调用@1201程序,现在,我没有使用
    cmake
    ,只是使用
    g++-std=c++11 main.cpp
    命令运行我的程序。那么,可以在该命令中设置警告级别吗?感谢您的回答,现在我没有使用
    cmake
    ,只是使用
    g++-std=c++11 main.cpp
    命令运行我的程序。所以,我没有收到任何编译器警告!是否可以在我使用的命令中获得此信息?至少使用
    -Wall
    开关,但您也可以检查此项:
    class Plane : public Vect3 {
    public:
        Plane(std::vector<float> plane_coefficients);
    
    private:
        float _a = 0.0;
        float _b = 0.0;
        float _c = 0.0;
        float _d = 0.0;
    };
    
    Plane::Plane(std::vector<float> plane_coefficients) : _a(plane_coefficients[0]), _b(plane_coefficients[1]), _c(plane_coefficients[2]), _d(plane_coefficients[3]), Vect3(_a, _b, _c) {
        std::cout << "from Plane class: " << _x << " " << _y << " " << _z << "\n";
    }
    
    int main() {
        std::vector<float> vec1 = {1.0, 1.0, 1.0, 1.0};
    
        Plane plane1(vec1);
        plane1.printVect3Magnitude();
        plane1.printAngleWrtAxisDeg();
    
        std::cout << "= = = = = = = = = = = =\n";
    
        Vect3 vect3d(vec1);
        vect3d.printVect3Magnitude();
        vect3d.printAngleWrtAxisDeg();
    
        return 0;
    }
    
    from Plane class: 3.07795e-41 2.8026e-45 0
    0
    Angle in degree, wrt X-axis = nan | wrt Y-axis = nan | wrt Z-axis = -nan
    = = = = = = = = = = = =
    1.73205
    Angle in degree, wrt X-axis = 54.7356 | wrt Y-axis = 54.7356 | wrt Z-axis = 54.7356
    
    Plane::Plane(std::vector<float> plane_coefficients) : _a(plane_coefficients[0]), _b(plane_coefficients[1]), _c(plane_coefficients[2]), _d(plane_coefficients[3]), Vect3(plane_coefficients) {
        std::cout << "from Plane class: " << _x << " " << _y << " " << _z << "\n";
    }
    
    from Plane class: 1 1 1
    1.73205
    Angle in degree, wrt X-axis = 54.7356 | wrt Y-axis = 54.7356 | wrt Z-axis = 54.7356
    = = = = = = = = = = = =
    1.73205
    Angle in degree, wrt X-axis = 54.7356 | wrt Y-axis = 54.7356 | wrt Z-axis = 54.7356