C++ 中通过引用传递的两种方式?
我正在学习关于参考参数,得到以下问题:
这和这有什么区别
void B(int* worthRef) {
*worthRef += 1;
}
int main() {
int netWorth = 55;
B(&netWorth);
return 0;
}
:
void B(int &worthRef) {
worthRef += 1;
}
int main() {
int netWorth = 55;
B(netWorth);
return 0;
}
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
How to pass objects to functions in C++?
I am learning about reference parameters and got the following question:
What is the difference between this:
void B(int* worthRef) {
*worthRef += 1;
}
int main() {
int netWorth = 55;
B(&netWorth);
return 0;
}
And this:
void B(int &worthRef) {
worthRef += 1;
}
int main() {
int netWorth = 55;
B(netWorth);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个 (
int *
) 按值传递一个指向整数的指针;第二个 (int &
) 通过引用传递一个整数。这两种方法都可以用来实现抽象的“引用语义”,但在 C++ 中,您应该尽可能使用实际引用。当您使用指针实现引用语义时,您可以按值传递一个指针到您想要引用的对象,然后取消引用该指针以获得实际的对象引用。在 C 语言中,语言中没有引用,这是实现引用语义的唯一方法,但在 C++ 中,语言中有实际的引用类型用于此目的。
请注意,传递指针的使用方式与引用略有不同,因为您可以传递空指针来传达附加语义,并且还可以修改指针变量(例如,使用被调用者范围中指针的本地副本来传递其他语义)遍历数组)。
但简而言之: 使用参考文献。不要在 C++ 中使用裸指针。(要实现引用语义,您可以使用引用,并且要实现裸指针可以(滥用)使用的任何其他用途,请使用适当的高级习惯用法。)根本问题与裸指针不同的是,它们不传达所有权语义,并且 C++ 有许多更好的工具来编写可维护的、本地的和自文档化的代码。
The first one (
int *
) passes a pointer-to-integer by value; the second (int &
) passes an integer by reference. Both methods can be used to implement abstract "reference semantics", but in C++ you should use actual references when possible.When you use pointers to implement reference semantics, you pass by value a pointer to the object that you want to refer to, and you dereference the pointer to obtain the actual object reference. In C, where you have no references in the language, that's the only way to implement reference semantics, but in C++ you have actual reference types in the language for this purpose.
Note that passing a pointer can be used a little differently from a reference, since you can pass a null pointer to convey additional semantics, and you can also modify the pointer variable (e.g. for using the local copy of the pointer in the callee scope to traverse an array).
But in a nutshell: Use references. Don't use naked pointers in C++. (To implement reference semantics, you use references, and to implement whatever else naked pointers can be (ab)used for, use the appropriate higher-level idiom.) The fundamental problem with naked pointers is that they convey no ownership semantics, and C++ has many far better tools to write maintainable, local and self-documenting code.
这里传递的是 int 的地址:
该参数是一个指针。传递的地址可能是
0
(或NULL
)。也用于 C 中。指针可以在B
中更改:++worthRef
- 为什么您会更喜欢...这里,实现采用参数的地址
:参数是 C++ 参考。传递的地址不得为
0
,并且不得更改(当然,如果不是 const,则它所引用的内容可能会更改,如示例所示)。这是大多数 C++ 圈子中默认的书写风格。从机制上讲,引用是一点语法糖,但它对于传达意图和提供保证绝对有用。从风格上来说,有些人更喜欢前者,因为参数可能会发生变化。我尽可能使用后者(并且合法)——它对于 C++ 来说更为传统。
Here you are passing the address of the int:
The parameter is a pointer. The address passed may be
0
(orNULL
). Also used in C. The pointer may altered withinB
:++worthRef
- why you would prefer that...Here, the implementation takes the address of the parameter:
The parameter is a C++ reference. The address passed must not be
0
, and may not be altered (of course, what it refers to may be altered if not const, as seen in your example). This is the default written style in most C++ circles. Mechanically, a reference is a bit of syntactic sugar, but it is absolutely useful to convey intent and offer guarantees.Stylistically, some people prefer the former for parameters which may mutate. I use the latter wherever possible (and legal) -- it's more conventional for C++.