初始化相等 C++ 的顺序
Order of initializing an equality C++
这个程序做两个整数的加法,程序完美运行(练习要求我使用构造函数和过去的整数)。
但是在构造函数上,如果我初始化 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;
}
The C++ assignment operator =
将左手值设置为等于右手值。语句 num1 = nbre1
将 num1 设置为 nbre1 中未初始化的值。如果 nbre1 之前已初始化,该语句会将 num1 设置为该值,但由于 num1 未在该函数的其他任何地方使用,因此它实际上什么都不做。
这个程序做两个整数的加法,程序完美运行(练习要求我使用构造函数和过去的整数)。
但是在构造函数上,如果我初始化 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;
}
The C++ assignment operator =
将左手值设置为等于右手值。语句 num1 = nbre1
将 num1 设置为 nbre1 中未初始化的值。如果 nbre1 之前已初始化,该语句会将 num1 设置为该值,但由于 num1 未在该函数的其他任何地方使用,因此它实际上什么都不做。