What is a good way to think about C ++ links?

I have been programming C, mainly in the embedded environment, for many years and have a very good mental model of pointers - I don’t have to think explicitly about how to use them, it is 100% more convenient with pointer arithmetic, pointer arrays, pointer pointers, etc. d.

I have written very little C ++ and really am not very good at links. In the past, I was advised to "think of them as pointers that cannot be NULL," but this question shows that this is far from a complete story.

So, for more experienced C ++ programmers - how do you feel about links? Do you think of them as a special index or as your own goal? What is a good way to program a C programmer?

+7
c ++ reference
source share
12 answers

I used to think of links as an alias for the main object.

EDIT (due to request in comments):

I used to think of a link as an alias, because it behaves exactly the same as the original variable, without having to do extra manipulation to affect the reference variable.

+12
source share

For me, when I see a pointer in code (as a local variable in a function or a member of a class), I have to think about

  • Is the pointer null or is it valid
  • Who created the object that it points to (is it me?), I did it yet?)
  • Who is responsible for the removal of the Object
  • Does this always indicate the same Object

I do not need to think about any of this material, if this is a link, this is some other problem (i.e. think of the link as a SEP Field for a pointer)

PS Yes, this is probably still my problem, just not now.

+8
source share

I don’t really like the ubiquitous look, as links can become invalid, for example.

int* p = new int(100); int& ref = *p; delete p; // oops - ref now references garbage 

So, I think the links are not rewritable (that is, you cannot change the purpose of the link after it is initialized) with syntactic sugar pointers to help me get rid of the syntax of the "->" pointer.

+6
source share

In general, you just don't think about links. You use links in each function if you do not need to call by value or pointer magic.

Links are essentially pointers that always point to the same thing. The link does not need to be dereferenced, and instead, it can be obtained as a regular variable. That is almost all there is. You use pointers when you need to do pointer arithmetic or change what the pointer points to, and links for everything else.

+5
source share

References are constant pointers with different syntaxes. i.e. the reference T & is pretty much T * const as in, the pointer cannot be changed. The contents of both are identical - the memory address is T - and none of them can be changed.

Then besides this, to a large extent, the only difference is the syntax :. for links and → and * for a pointer.

What it really does is refers to ARE pointers, just with a different syntax (and they are constants).

+3
source share

How about "pointers that cannot be NULL and cannot be changed after initialization." In addition, they themselves do not have a size (because they have no personality).

+2
source share

I consider the link a reference to the object to which it refers. You are using access to the object. symantecs (as opposed to →), reapplying this idea to me.

+2
source share

I think your mental pointer model, and then a list of all the edge cases you come across, is the best way.

Those who do not get pointers will be much worse.

By the way, they can be NULL or any other inaccessible memory cell (this just takes effort):

 char* test = "aha"; char& ok = *test; test = NULL; char& bad = *test; 
+2
source share

One way to think about them is to import another name for the object from a possibly different area.

For example: Obj o; Obj& r = o; Obj o; Obj& r = o; There really are few differences between the semantics of o and r.

Most importantly, the compiler monitors volume o to invoke the destructor.

+1
source share

I view it as a pointer container.

0
source share

From syntactic POV, a reference is an alias for an existing object. From the semantic POV, this link behaves like a pointer with several problems (annulment, ownership, etc.) And an object-like syntax is added. From a practical POV, prefer links if you don’t need to say “no object”. (Resource ownership is not a preference for pointers, as this should be done using smart pointers.)

Refresh . Here's another difference between references and pointers that I forgot about: a temporary object (rvalue) associated with a constant reference will have a lifespan extended to the lifespan of the link:

 const std::string& result = function_returning_a_string(); 

Here, the temporary function returned by the function is bound to result and will not cease to exist at the end of the expression, but will exist until result dies. This is nice, because in the absence of references to rvalue and overloads based on them (as in C ++ 11), this allows you to get rid of one unnecessary copy in the above example.

This rule is introduced specifically for const references, and there is no way to achieve this with pointers.

0
source share

If you use Linux, you can treat links as hard links and pointers as symbolic links (symbolic links). A hard link is just another file name. A file is deleted when all hard links to this file are deleted.

The same goes for links. Just replace the "hard link" with "link" and "file" with "value" (or perhaps a "memory location"?).

A variable becomes destroyed when all links go beyond.

You cannot create a hard link to a file that does not exist. Similary, it’s impossible to link to anything.

However, you can create a symbolic link to a nonexistent file. Like an uninitialized pointer. Actually uninitialized pointers point to some random locations (correct me if I am wrong). But I mean that you should not use them :)

0
source share

All Articles