c ++ 传递可变长度数组_学习C ++:可变范围

本文探讨了C++中如何传递可变长度数组,重点在于理解其内存管理和参数传递的方式。通过翻译自https://levelup.gitconnected.com/learning-c-variable-scope-8e11cd49cb4e的文章,读者可以学习到C++处理可变长度数组的技巧。
摘要由CSDN通过智能技术生成

c ++ 传递可变长度数组

Variable scope refers to the ability of a variable to be “seen” in a program. A variable is seen if, in a specific place in a program, the programmer has access to that variable’s value. There are levels of scope in a C++ program, with the level of scope determining how widely a variable can be seen in a program. In this article I’m going to discuss the different levels of variable scope and why they are important.

变量范围是指变量在程序中“可见”的能力。 如果程序员在程序中的特定位置可以访问该变量的值,则可以看到该变量。 C ++程序中有范围级别,范围级别决定了变量在程序中的可见程度。 在本文中,我将讨论变量作用域的不同级别以及它们为什么很重要。

范围级别 (The Scope Levels)

Here are the three levels of scope in a C++ program: global, local, and block. Global is the most expansive scope and means that any variable declared with global scope can be seen in any place in a program. Local scope usually refers to a variable defined in a function and can be seen throughout that function, but not in other functions. Block scope means a variable was defined within a block of code, such as a for loop or an if statement.

这是C ++程序的作用域的三个级别:全局,局部和块。 全局是最广泛的范围,意味着用全局范围声明的任何变量都可以在程序的任何位置看到。 局部作用域通常是指在函数中定义的变量,并且可以在该函数中看到该变量,但不能在其他函数中看到。 块作用域意味着在代码块内定义了一个变量,例如for循环或if语句。

With these definitions out of the way, let’s look at how these different scope levels affect a C++ program.

通过这些定义,让我们看一下这些不同的作用域级别如何影响C ++程序。

全球范围 (Global Scope)

A variable that has global scope can be seen in any other part of a C++ program, from a function definition to a block of code. A variable that is defined outside of the main function is a global variable.

从函数定义到代码块,可以在C ++程序的任何其他部分中看到具有全局范围的变量。 在主函数之外定义的变量是全局变量。

Here is an example that demonstrates how global scope works:

下面的示例演示了全局作用域如何工作:

#include <iostream>
using namespace std;// global spaceint number = 1;void showValue() {
cout << "Number accessed from a function: " << number << endl;
}int main ()
{
showValue();
cout << "Number accessed from the main function: "
<< number << endl;
for (int i = 1; i <= 1; i++) {
cout << "Number accessed from a block: " << number << endl;
}
return 0;
}

Here is the output from this program:

这是该程序的输出:

Number accessed from a function: 1
Number accessed from the main function: 1
Number accessed from a block: 1

The primary problem with global variables is that, since they can be accessed from anywhere else in a program, they can be changed anywhere else in a program, which leads to hard-to-find logic errors. It also makes it harder to read and understand a program when it uses global variables because the reader is constantly having to go back to the global space to see where the variable originated from.

全局变量的主要问题在于,由于可以从程序中的其他任何地方访问它们,因此可以在程序中的其他任何地方对其进行更改,从而导致难以发现的逻辑错误。 当使用全局变量时,这也使阅读和理解程序变得更加困难,因为读者不断需要回到全局空间来查看变量的来源。

局部变量 (Local Variables)

A local variable is a variable that is defined within a function definition. Variables that are defined within a function are visible only to code that exists within the function definition. The variable is said to be “local” to that function.

局部变量是在函数定义中定义的变量。 函数中定义的变量仅对函数定义中存在的代码可见。 据说该变量是该函数的“局部”变量。

Here is an example that demonstrates how local scope works:

这是一个示例,演示本地作用域如何工作:

// global spaceint number = 1;void defineNumber() {
int number = 2;
cout << "Number accessed from defining function: "
<< number << endl;
}int main ()
{
cout << "Accessing global number: " << number
<< endl; // this is the global variable
defineNumber(); // this is the local version
number = 2; // changing global variable
cout << "Accessing global number: " << number
<< endl; // this is the global variable
return 0;
}

Here is the output from this program:

这是该程序的输出:

Accessing global number: 1
Number accessed from defining function: 2
Accessing global number: 2

The function defineNumber defines a variable named number but at a local scope rather than a global scope. The code in main accessed the global number for the first output statement, calls the defineNumber function to display that number variable’s value, then assigns a new value to the global number.

函数defineNumber定义一个名为number的变量,但变量位于本地范围而不是全局范围。 main的代码访问了第一个输出语句的全局编号,调用defineNumber函数以显示该编号变量的值,然后为该全局编号分配一个新值。

This example demonstrates the confusion that can be caused when you use global variables.

本示例说明了使用全局变量时可能引起的混乱。

区块范围 (Block Scope)

The third scope level is block scope. Variables defined in a code block can only be seen inside that block. A block is any code wrapped inside curly braces. Of course, the global space and function definitions have blocks, but for block-level scope we are generally talking about an even tighter space of code, such as a for loop.

第三作用域级别是块作用域。 在代码块中定义的变量只能在该块内部看到。 块是包装在花括号中的任何代码。 当然,全局空间和函数定义都有块,但是对于块级范围,我们通常谈论的是更紧密的代码空间,例如for循环。

The following short program demonstrates the differences between global, local, and block scope:

以下简短程序演示了全局,局部和块作用域之间的区别:

// global spaceint number = 1;int main ()
{
cout << "Global number: " << number << endl;
int number = 2;
cout << "Local number: " << number << endl;
{
int number = 3;
cout << "Block number: " << number << endl;
}
return 0;
}

The output from this program is:

该程序的输出为:

Global number: 1
Local number: 2
Block number: 3

The code in main demonstrates that you can create a block that is nothing but just code, it doesn’t have to be attached to a construct such as a loop.

main的代码演示了您可以创建一个仅是代码而已的块,而不必将其附加到诸如循环之类的构造中。

对于循环和块作用域 (For Loops and Block Scope)

One of the most important uses of block-level scoping is to constrain the value of the loop’s controlling variable. Many times you will see a for loop written like this:

块级作用域最重要的用途之一就是约束循环控制变量的值。 很多时候,您会看到如下所示的for循环:

int main ()
{
const int numElements = 5;
int grades[numElements] = {81, 77, 91, 88, 78};
int i = 0;
for (; i < numElements; i++) {
cout << grades[i] << " ";
}
return 0;
}

For whatever reason, the author didn’t choose to take advantage of block-level scoping in the for loop. This means the variable i is visible and accessible outside the loop and can’t be used again without reassigning its value.

无论出于什么原因,作者都没有选择在for循环中利用块级作用域。 这意味着变量i在循环外可见且可访问,并且在不重新分配其值的情况下无法再次使用。

The better way is to always make the loop control variable block level by initializing it inside the loop:

更好的方法是通过在循环内部进行初始化来始终使循环控制变量块级别:

int main ()
{
const int numElements = 5;
int grades[numElements] = {81, 77, 91, 88, 78};
for (int i = 0; i < numElements; i++) {
cout << grades[i] << " ";
}
return 0;
}

By defining the loop control variable, i, as a block-scoped variable, I can also get away with making its length a single letter. This is not considered good programming practice but is acceptable in short blocks such as this one where the variable’s initialization statement is close to its use in the program.

通过将循环控制变量i定义为块范围变量,我也可以避免将其长度设为单个字母。 这不是一种好的编程习惯,但是在诸如此类变量变量的初始化语句接近其在程序中使用的此类短代码块中是可以接受的。

功能参数范围 (The Scope of Function Parameters)

Function parameters have local scope. This means that they are visible inside a function definition but nowhere else. This makes perfect sense since the function definition has to be able to access its parameter’s values in order to use them in the function.

函数参数具有局部范围。 这意味着它们在函数定义中可见,而在其他地方则不可见。 这非常合理,因为函数定义必须能够访问其参数的值才能在函数中使用它们。

如何使用可变范围 (How to Use Variable Scope)

Your goal as a programmer is to keep your variables under the tightest scope you can manage without being ridiculous. Many variables are going to be defined as local by necessity but you should always be looking to define variables at the block level when you can. Certainly, for loop control variables should be defined in the block and other loop variables that aren’t needed elsewhere should be defined as block variables also.

作为程序员的目标是将变量保持在可以管理的最严格范围内,而不会太荒谬。 许多变量将根据需要定义为局部变量,但是您应该始终在可能的情况下在块级别定义变量。 当然,对于循环控制变量,应在块中定义,而在其他地方不需要的其他循环变量也应定义为块变量。

However, beware defining variables in if statements at block level. You will often receive error messages from the compiler saying that the variable or variables defined inside the if are not reachable. This is because the variable may not be used if the condition being tested isn’t true and the variable declaration was placed in that part of the if statement.

但是,请注意在块级的if语句中定义变量。 您通常会收到来自编译器的错误消息,指出if中定义的一个或多个变量不可访问。 这是因为如果要测试的条件不正确,并且变量声明放置在if语句的该部分中, if可能不会使用该变量。

Variable scope is sometimes confusing to new programmer students but it is a very important topic and one you should make sure you understand as you progress in your programming education.

可变范围有时会使新的程序员学员感到困惑,但这是一个非常重要的话题,您应该确保自己随着编程教育的进步而了解。

Thanks for reading and please email me with comments and suggestions. If you are interested in my online programming courses, please visit https://learningcpp.teachable.com.

感谢您的阅读,请给我发送评论和建议。 如果您对我的在线编程课程感兴趣,请访问https://learningcpp.teachable.com

翻译自: https://levelup.gitconnected.com/learning-c-variable-scope-8e11cd49cb4e

c ++ 传递可变长度数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值