开发者

C++ references Vs C# references [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

What are the similarties/differences betwe开发者_如何学Pythonen a C++ reference and a C# reference ?

Edit:

I'm talking about Object references,as I'm a newbie I was unaware that such a simple line would cause ambiguity,as I have read. Whenever the term "reference" is used,its in context to Object reference,otherwise its explicitly quoted as "managed references".

I think all the people who have answered this question have got what I was trying to say and I made a comment that states clearly what I wanted to ask. I don't see any reason for a downvote,come on guys.

This question does not deserve to be closed.As newbies like me can learn from the insight a lot of experienced people have provided.


C# references are closer to C++ pointers than to C++ references.

C# references can be null and have assignment semantics similar to pointers. The difference is that they are black boxes(you can't convert them to integral types) and you can't do arithmetic on them. They also can only point to reference types.

C++ references are quite different(apart from being compiled to pointers too). They are closer to .net managed references which are used for ref parameters. But even then the assignment semantics differ.


Initialization of a reference is something quite different from assignment to it. Despite appear- ances, no operator operates on a reference.


The are not very much alike.

The only thing in C# that looks like a C++ reference are parameters with the ref keyword. There are no equivalents of reference variables or fields.

C# reference are in most respects much more like C++ pointers. They can be null etc.

Edit

In C# the term "a reference" always means an object-reference. De-referencing is automatic, that can make comparing with C++ a little difficult:

string h = "hello";
int len = h.Length;   // dereference, Length is a property of the instance
h = null;             // no de-referencing, h itself becomes null.


Update:

Apparently I'd misunderstood the question: I'd understood the word "reference" to be referring to "managed references", not "object references". "Managed references" (preferably called "managed pointers", which I did not do originally) are the feature that allows something like ref int in C#, and are very similar to C++ references. They are a .NET Framework concept, however -- not a C# concept. By constrast, an "object reference" (or just a "reference") is simply referring to a reference type.

In my answer below, I'm referring to managed references/pointers, not objects.


In the implementation, they're almost exactly the same thing: just fancy pointers that aren't supposed to be null.

In the code, they're somewhat different: for one thing, you can't have a "reference variable" in C#, but you can in C++.

i.e., you can say

int x = 5;
int& y = x;

in C++, but you can't say:

int x = 5;
ref int y = x;

in C#.

But the usual assignment semantics apply: Assigning to a reference assigns to the target; there's no way to change the reference itself in C# or C++.


However, it's technically not entirely disallowed entirely by the .NET framework to have a "reference to an int"; in fact, such a type does exist. But no .NET language (other than pure IL) has the ability to declare a variable to have the type of a managed reference, as far as I know.


The question seem stated unprecisely. After reading all the answers and comments I am still not sure if any of them really clarified things.

One of the major simplifications of C# (or the CLR type system) over native C++ is the clear separation into value and reference types with the possibility to pass both either by value or by reference.

Somewhat simplified, C++ on the other hand, only knows "value types" where the application has to decide whether their lifetime is managed automatically or manually. In C++ the term "reference type" denotes a new type declared by writing T& where T is a valid type name. In C++ a reference is a subtle thing: The declaration T& defines an "l-value reference to T" while T&& defines "r-value reference to T" (reference to a temporary object, new concept introduced by c++0x) both beeing different types. The mutual binding rules (if passed as an argument, returned from a function, or used as an l-value or r-value in an assignment expression) of

  • T
  • T const &
  • T const &&
  • T&
  • T&&

are a nightmare although they open possibilities for some very useful and powerful paradigms. Of course, since T* (a pointer type) can be substituted for T in T& or any of it's alternatives you can define a "reference to a pointer-type" type with all the combinations described above. The opposite, however, is not possible, a "pointer to a reference type" is not allowed.

Huh, I hope I confused everything even more...

Cheers,

Paul

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜