Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.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++ - Fatal编程技术网

C++ C+中程序的结果+;包括全局变量和局部变量

C++ C+中程序的结果+;包括全局变量和局部变量,c++,C++,我用C++语言编写了以下代码: #include <iostream> using namespace std; int num1(1); int num2(2); int foo1(int val) { val = val + 1; return val; } int foo2(int& val) { val = val + 1; num1 = num2 + 1; return val - 1; } int foo3(int& val)

我用C++语言编写了以下代码:

#include <iostream>
using namespace std;

int num1(1);
int num2(2);

int foo1(int val) {
  val = val + 1;
  return val;
}

int foo2(int& val) {
  val = val + 1;
  num1 = num2 + 1;
  return val - 1;
}

int foo3(int& val) {
  int num2(2);
  num2 = num2 + 1;
  return foo1(3*num1) + foo2(val) ;
}

int main() {
  int num1(3);
  cout << "Resultat 1: " << foo3(num1) << endl;
  cout << "Resultat 2: " << num1 << endl;
  cout << "Resultat 3: " << num2 << endl;
}
在主干道的尽头

编辑:很抱歉,我的问题似乎不适合这个论坛;我是新来的。根据评论中的建议,我对其进行了编辑,并得到:

Resultat 1: 7
Resultat 2: 4
Resultat 3: 2
我想我知道会发生什么;局部变量和全局变量被视为不同的变量;至少这样的结果是有意义的。这是真的吗?如何解释这一点,即为什么这对编译器来说不是一个模棱两可的问题

我应该补充一点,这个练习来自于一次纸上考试,所以实际上不能简单地编译程序

如果这个问题仍然不合适,请留下评论,我会尽力解决。

变量有效的“程序中的部分”称为变量范围。 C++是灵活的,允许变量重写。 这里有一些解释,但最终你必须自己玩才能找到答案

示例中最好地显示了这一点:

#include <iostream>
using namespace std;

int num1(1); //this is global scope
int num2(2); //so is this
//global variables are available inside all functions including main


int foo1(int val) {  //num1 and num2 are available here
  val = val + 1;  //val is a parameter passed by value. it is just a copy
  //what you do to val here will not stick to the variable that was passed in
  return val;
}

int foo2(int& val) {
  val = val + 1;   //val is a parameter passed by reference, what you do to it here will stick
  num1 = num2 + 1; // global variables are available to be manipulated
 // (changes will stick because there is no copy in function)

  return val - 1;   //(val-1) is a temporary value (rvalue), it does not affect value of val;
}

//val is not available here, because it is only a function parameter valid in the bodies of foo1,foo2 and foo3.

int foo3(int& val) { //val is an integer again passed by reference, means changing it will change the variable that was sent to this function.
  int num2(2); //this is a variable declared in the scope of this function, 
 //it is only available in this function, when this function returns
 //then num2 is destroyed.

 //this is generally not considered good practice, because you are masking 
 //a global variable, from now on, num2 refers to the local copy
 //if you want the global num2 you have to use ::num2 

  num2 = num2 + 1; //whatever we do to num2 is for the local copy only
 //becuase we overrode the name.(usually bad idea)

  return foo1(3*num1) + foo2(val) ; 
}

int main() {
 //global num1 is available here 

  int num1(3); //here is another example of overriding a global variable. 

  //at this point num1 no longer refers to the global variable,
  // instead it refers to the local variable declared in main. 
  //if you want global variable num1 you should use ::num1

  cout << "Resultat 1: " << foo3(num1) << endl;  //passes the local variable num1 by reference. it will take the name "value" inside foo3
  cout << "Resultat 2: " << num1 << endl;
  cout << "Resultat 3: " << num2 << endl;
}
#包括
使用名称空间std;
int num1(1)//这是全球范围
int num2(2)//这也是
//全局变量在所有函数(包括主函数)中都可用
intfoo1(intval){//num1和num2在这里可用
val=val+1;//val是通过值传递的参数。它只是一个副本
//您在这里对val所做的操作不会粘附到传入的变量
返回val;
}
int foo2(int&val){
val=val+1;//val是一个通过引用传递的参数,您在这里对它所做的操作将保持不变
num1=num2+1;//全局变量可供操作
//(更改将保持不变,因为没有“复制入”功能)
返回val-1;//(val-1)是一个临时值(rvalue),它不影响val的值;
}
//val在这里不可用,因为它只是在foo1、foo2和foo3的主体中有效的函数参数。
int foo3(int&val){//val是通过引用再次传递的整数,这意味着更改它将更改发送到此函数的变量。
int num2(2);//这是在此函数范围内声明的变量,
//它仅在该函数返回时在此函数中可用
//然后num2被销毁。
//这通常被认为是不好的做法,因为您正在屏蔽
//一个全局变量,从现在起,num2指的是本地副本
//如果需要全局num2,则必须使用::num2
num2=num2+1;//我们对num2所做的任何操作都只用于本地副本
//因为我们超越了这个名字。(通常是坏主意)
返回foo1(3*num1)+foo2(val);
}
int main(){
//此处提供全局num1
int num1(3);//这里是重写全局变量的另一个示例。
//此时num1不再指全局变量,
//相反,它引用main中声明的局部变量。
//如果需要全局变量num1,应该使用::num1

你能不能先看看发生了什么,然后再倒过来看看,这样你就能明白发生了什么。如果你真的想知道一段代码是如何工作的,那么你应该用调试器一步一步地检查代码。这将让你看到程序每一行都发生了什么。你不必在中编写
返回0
<代码>主< /代码>因为它是C++中不需要的。因为它对制作和运行程序不感兴趣,所以投票关闭,其他人也不感兴趣。“我很困惑……”是的,这就是练习的重点。你必须弄明白。非常感谢你花时间回答我的问题,我现在明白了。+1有太多东西很难解释所有编写的内容。而仅仅通过反复阅读解决方案就更难理解了。你应该去玩你的编译器,试着去理解它了解每一步中每个变量都发生了什么。更好的是,使用gdb之类的调试器。如果你不能向自己证明为什么一个变量有一个值,那么请回来阅读我的评论。你应该首先单独测试foo1和foo2,完全了解它们在做什么。然后选择foo3
#include <iostream>
using namespace std;

int num1(1); //this is global scope
int num2(2); //so is this
//global variables are available inside all functions including main


int foo1(int val) {  //num1 and num2 are available here
  val = val + 1;  //val is a parameter passed by value. it is just a copy
  //what you do to val here will not stick to the variable that was passed in
  return val;
}

int foo2(int& val) {
  val = val + 1;   //val is a parameter passed by reference, what you do to it here will stick
  num1 = num2 + 1; // global variables are available to be manipulated
 // (changes will stick because there is no copy in function)

  return val - 1;   //(val-1) is a temporary value (rvalue), it does not affect value of val;
}

//val is not available here, because it is only a function parameter valid in the bodies of foo1,foo2 and foo3.

int foo3(int& val) { //val is an integer again passed by reference, means changing it will change the variable that was sent to this function.
  int num2(2); //this is a variable declared in the scope of this function, 
 //it is only available in this function, when this function returns
 //then num2 is destroyed.

 //this is generally not considered good practice, because you are masking 
 //a global variable, from now on, num2 refers to the local copy
 //if you want the global num2 you have to use ::num2 

  num2 = num2 + 1; //whatever we do to num2 is for the local copy only
 //becuase we overrode the name.(usually bad idea)

  return foo1(3*num1) + foo2(val) ; 
}

int main() {
 //global num1 is available here 

  int num1(3); //here is another example of overriding a global variable. 

  //at this point num1 no longer refers to the global variable,
  // instead it refers to the local variable declared in main. 
  //if you want global variable num1 you should use ::num1

  cout << "Resultat 1: " << foo3(num1) << endl;  //passes the local variable num1 by reference. it will take the name "value" inside foo3
  cout << "Resultat 2: " << num1 << endl;
  cout << "Resultat 3: " << num2 << endl;
}