初始化相等C ++的顺序

问题描述 投票:0回答:1

该程序将两个整数相加,该程序运行完美(练习要求我在上面使用构造函数和过去的整数)。但是在构造函数上,如果我初始化num1 = nbre1而不是nbre1 = num1,则该程序将无法运行。关于订单的任何解释吗?

#include <iostream>

int main(){

    class op{
        public :
            int nbre1, nbre2;
            op(int num1, int num2){
                nbre1=num1;
                nbre2=num2;
                std::cout<<"numbers initialized";
            }
            int add(){return nbre1+nbre2 ;}     
    };

    int n1;
    int n2;
    std::cout<<"Enter the first integer >> ";
    std::cin>>n1;
    std::cout<<"\n";
    std::cout<<"Enter the second integer >> ";
    std::cin>>n2;

    op addition(n1,n2);
    std::cout<<"The sum of the two numbers is >> " << addition.add();


    return 0;
}
c++ function class constructor initialization
1个回答
0
投票

The C++ assignment operator =将左手值设置为等于右手值。语句num1 = nbre1将num1设置为nbre1中的未初始化值。如果nbre1先前已初始化,则该语句将num1设置为该值,但是由于num1在该函数的其他任何地方均未使用,因此它实际上什么也不做。

© www.soinside.com 2019 - 2024. All rights reserved.