gpt4 book ai didi

c# - C++ 引用类型参数传递和 C# 的引用之间的区别?

转载 作者:搜寻专家 更新时间:2023-10-31 00:06:02 25 4
gpt4 key购买 nike

我一直认为它们是关于同一件事的,但有人在我的一个回答中指出我这不是真的。

编辑:Here是我所说的和我得到的评论。编辑2:C++ 的区别是什么:

public: void foo(int& bar);

和C#的

public void foo(ref int bar){

}

最佳答案

在 C# 中,您有原始类型(整数、结构等)和引用类型(对象)。这些是内置于语言中的。在 C++ 中,你必须是显式的。下面是一组在 C# 和 C++ 中引用对象的等效方法,具体取决于它们是引用类型还是原语,以及您是否使用 ref:

C# type                     | C++ Type
----------------------------+---------
Primitive type, with no ref | Normal parameter passing
Reference type, with no ref | Pointer (*)
Primitive type, with ref | Reference (&)
Reference type, with ref | Reference of a pointer (&*)

关于c# - C++ 引用类型参数传递和 C# 的引用之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/924360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com