C ++和C#中指针和函数参数之间的等价性[关闭](Equivalency between pointers and function parameters in C++ and C# [closed

编程入门 行业动态 更新时间:2024-10-26 16:31:07
C ++和C#中指针和函数参数之间的等价性[关闭](Equivalency between pointers and function parameters in C++ and C# [closed])

如果以下任何理解不准确,请与我们联系。

MyClass m = new MyClass() <---> MyClass * m = new MyClass; ??? (没有C#等价物)<---> MyClass m(5,"foo"); void SomeMethod(ref Widget w) <---> void SomeMethod(Widget & w)或void SomeMethod(Widget * * w) void SomeMethod(Widget w) <---> void SomeMethod(Widget * w) ??? (没有C#等价物)<---> void SomeMethod(const Widget) ??? (没有C#等价物)<---> void SomeMethod(const & Widget) void SomeMethod(out Widget w) <---> ??? (没有C ++等价物)

Let me know if any of my following understanding is inaccurate.

MyClass m = new MyClass() <---> MyClass * m = new MyClass; ??? (no C# equivalent) <---> MyClass m(5,"foo"); void SomeMethod(ref Widget w) <---> void SomeMethod(Widget & w) or void SomeMethod(Widget * * w) void SomeMethod(Widget w) <---> void SomeMethod(Widget * w) ??? (no C# equivalent) <---> void SomeMethod(const Widget) ??? (no C# equivalent) <---> void SomeMethod(const & Widget) void SomeMethod(out Widget w) <---> ??? (no C++ equivalent)

最满意答案

C#和C ++完全不同,超出了他们的名字所暗示的,但无论如何我都会尝试:

MyClass m = new MyClass() <---> MyClass * m = new MyClass;

大致相当,除了C#将在不再使用后收集m 。 使用C ++,您必须记住delete该对象。 在C ++中,分配给堆栈或使用智能指针更为自然。

??? (no C# equivalent) <---> MyClass m(5,"foo");

C#并没有给你很多控制对象分配的位置。 但是,它具有可以避免堆分配的struct类型。

void SomeMethod(ref Widget w) <---> void SomeMethod(Widget & w) or void SomeMethod(Widget * * w)

这些是类似的,但在C# ref通常应该避免。

void SomeMethod(Widget w) <---> void SomeMethod(Widget * w)

这里的主要区别在于,在C ++中,任何具有指向对象的指针的人都可以删除它。 在C#中,垃圾收集器处理删除。 C#有点像使用std::shared_ptr ,但有循环检测。

??? (no C# equivalent) <---> void SomeMethod(const Widget) ??? (no C# equivalent) <---> void SomeMethod(const & Widget)

C#没有像C ++那样强大的const概念。 通常,必须使用每个字段上的readonly关键字使整个类型不可变。

void SomeMethod(out Widget w) <---> ??? (no C++ equivalent)

在C ++中,通过引用获取对象可以实现与out变量相同。 out变量通常是不好的做法,所以这个关键字是一种文档形式。

最重要的是, 花时间了解代码是如何用每种语言编写的 。 许多概念是共享的,但要真正利用技术,你应该使用它,而不是试图强制一种范式进入另一种范式。

C# and C++ are quite different, more than their names would suggest, but I will try anyway:

MyClass m = new MyClass() <---> MyClass * m = new MyClass;

Roughly equivalent, except that C# will collect m after it is no longer in use. With C++ you must remember to delete the object. In C++ it is more idomatic to allocate to the stack or use a smart-pointer.

??? (no C# equivalent) <---> MyClass m(5,"foo");

C# does not give you much control over where objects are allocated. However, it has struct types that can avoid heap allocation.

void SomeMethod(ref Widget w) <---> void SomeMethod(Widget & w) or void SomeMethod(Widget * * w)

These are similar, but in C# ref should usually be avoided.

void SomeMethod(Widget w) <---> void SomeMethod(Widget * w)

The main difference here is that in C++ anyone with a pointer to an object may delete it. In C# the garbage collector handles the deletion. C# is a bit like using std::shared_ptr, but with cycle-detection.

??? (no C# equivalent) <---> void SomeMethod(const Widget) ??? (no C# equivalent) <---> void SomeMethod(const & Widget)

C# does not have a powerful notion of const like C++ does. Typically the entire type must be made immutable using the readonly keyword on each field.

void SomeMethod(out Widget w) <---> ??? (no C++ equivalent)

In C++ taking an object by reference can achieve the same as out variables. out variables are usually bad practice, so this keyword is a form of documentation.

Most importantly, take the time to understand how code is meant to be written in each language. Many of the concepts are shared, but to really take advantage of a technology, you should work with it, rather than try to force one paradigm into another.

更多推荐

本文发布于:2023-07-15 19:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   函数   参数   Equivalency   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!