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

时间:2015-12-15 20:23:27

标签: 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) {
  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;
}

我必须回答的问题是程序将显示的内容。我很困惑,因为全局和局部变量具有相同的名称,我现在不知道如何处理这种歧义。答案是什么?由于我在编程方面很陌生,请给出一些解释。

Ps:为什么我们不必写

return 0;

在主要结尾?

编辑:对不起,我的问题似乎不适合这个论坛;我是新来的。按照评论中的建议我编译并得到:

Resultat 1: 7
Resultat 2: 4
Resultat 3: 2

我想我明白会发生什么;局部变量和全局变量被视为不同的变量;至少结果会以这种方式有意义。这是真的?如何解释这一点,即为什么这不是编译器的歧义?

我应该补充一点,这项练习来自纸上考试,所以实际上人们不能简单地编写程序。

如果问题仍然不合适,请发表评论,我会尝试解决。

1 个答案:

答案 0 :(得分:2)

程序中的&#34;部分&#34;变量有效的地方称为变量范围。 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;
}

这里还有另一个非常重要的教训:按值传递并通过引用语义传递。 按值获取参数的函数仅接收副本。函数体内对该变量的后续更改不会影响传入的参数。

相反,

是一个通过引用获取参数的函数,它将接收对变量&#34;的引用。这允许它直接修改变量。

相关问题