通过引用传递参数与使用实例变量
我开始阅读一本关于C++的书,发现了以下代码。 这是有关如何通过引用发送传递参数的示例。
#include <iostream>
void swap(int &x, int &y);
int main()
{
int x = 5, y = 10;
std::cout << "Main. Before swap, x: " << x
<< " y: " << y << "\n";
swap(x, y);
std::cout << "Main. After swap, x: " << x
<< " y: " << y << "\n";
return 0;
}
void swap(int &rx, int &ry)
{
int temp;
std::cout << "Swap. Before swap, rx: " << rx
<< " ry: " << ry << "\n";
temp = rx;
rx = ry;
ry = temp;
std::cout << "Swap. After swap, rx: " << rx
<< " ry: " << ry << "\n";
}
。
Main. Before swap, x:5 y: 10
Swap. Before swap, rx:5 ry:10
Swap. After swap, rx:10 ry:5
Main. After swap, x:10, y:5
我的逻辑很清楚。 现在这可能是一个非常愚蠢的问题(我还不是很有经验),但是为什么不能直接声明 private: int x 作为实例变量呢?在这种情况下,x 不是可以在类中的任何地方直接访问吗? (根本不需要指定参数)?预先感谢您的回答!
I started to read a book about C++ and found the following code.
It is an example on how you can send pass parameters by reference.
#include <iostream>
void swap(int &x, int &y);
int main()
{
int x = 5, y = 10;
std::cout << "Main. Before swap, x: " << x
<< " y: " << y << "\n";
swap(x, y);
std::cout << "Main. After swap, x: " << x
<< " y: " << y << "\n";
return 0;
}
void swap(int &rx, int &ry)
{
int temp;
std::cout << "Swap. Before swap, rx: " << rx
<< " ry: " << ry << "\n";
temp = rx;
rx = ry;
ry = temp;
std::cout << "Swap. After swap, rx: " << rx
<< " ry: " << ry << "\n";
}
.
Main. Before swap, x:5 y: 10
Swap. Before swap, rx:5 ry:10
Swap. After swap, rx:10 ry:5
Main. After swap, x:10, y:5
The logic is clear to me.
Now this may be a very stupid question (I'm not very experienced yet), but why can't you just declare private: int x as an instance variable? Isn't x in this case directly accessible everywhere in your class? (without the need for specifying parameters at all)? Thanks in advance for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出于几个原因。
只要您需要它们。
到)您错误使用的可能性就越大
变量,因此它的值可能会意外改变。这将是一个错误,祝你好运,找到那个错误。
类,并且您编写它,以便它使用实例成员 x (而不是方法
变量 x),那么该交换方法只能使用 x 进行交换,如果
当你需要它来交换不同的变量时(或者
类上另一个方法的参数)然后你必须移动
值放入 x 中,这是低效率的
转到 5
。使用您必须提供的值来调用交换函数,而不需要知道您必须首先设置一个特殊的 x 变量,这不是更好吗?是否使用交换方法? x 之后的值应该是多少
叫?您引入了许多有关交换的上下文并知道它何时发生
可以调用swap,什么可以调用swap。这很糟糕,任何代码片段越独立,就越不需要担心它以及它的使用方式。
交换方法必须实现它自己的,这是一个巨大的
不-不的原因比我在这里数得更多,但可以总结为
违反了DRY 原则
所有这些问题都可以通过简单地通过引用传递值来解决。真的很简单:)
希望这会有所帮助。
For several reasons.
around as long as you need them.
to) the greater the chance that you will mistakenly use the
variable, and therefore it's value may change unexpectedly. This will be a bug, good luck hunting that one down.
class, and you write it so it uses instance member x (not method
variable x), then that swap method CAN ONLY EVER swap using x, if
in time you need it to swap on a different variable (or the
parameter of another method on the class) then you've to move the
value into x which is Inefficient &
goto 5
. Isn't it better to call the swap function with the values you have to hand, without needing to know there's a special x variable that you have to set first?is using the swap method? What should the value of x be after it's
called? You're introducing lots of context around swap and knowing when it's
ok to call swap, and what can call swap. This is bad, the more self contained any piece of code is, then the less of have to worry about it, and about how it's used.
swap method must implement it's own, and this is a huge big
no-no for more reasons than I can count here, but can sum up as it
voliates the DRY Principal
All of these problems can be removed by simply passing the values by reference. Bit of a no-brainer really :)
Hope this helps.
通过参数向函数传递值可确保代码的模块化。听起来您刚刚开始使用 C++,所以我不确定您对面向对象编程有多熟悉。函数/方法代表一层封装。您的 swap() 函数应该封装执行其功能/目的所需的逻辑。调用者不应该关心这是如何完成的。如果您的 swap() 函数必须断言程序中存在可用的全局变量,那么它并没有完全封装“交换”的逻辑。
另外,假设您想在类中的其他地方重用此函数。使用一组全局变量来调用此函数会很困难且笨拙。此外,您的类中可能有其他位置引用这些全局变量,因此对 swap() 的其他调用将更改这些值,可能会导致代码其他区域的混乱。
Passing values via arguments to a function ensures modularity in your code. It sounds like you're just starting out with C++, so I'm not sure how familiar you are with object oriented programming. Functions/methods represent a layer of encapsulation. Your swap() function should encapsulate the logic needed to perform its function/purpose. The caller should not be concerned with how this is accomplished. If your swap() function must assert there is a global variable available in the program, then it's not fully encapsulating the logic of "swapping".
Also, Lets say you wanted to reuse this function elsewhere in your class. It would be difficult and clumsy to use a set of global variables for calling this function. In addition, you may have other locations in your class that are referencing those global variables, and therefore your other calls to swap() would change those values, potentially causing confusion in other areas of the code.