What does this statement mean? "Good C ++ programming usually doesn't use pointers in complex ways."

In this other question in the answer to the victory I read:

... good C ++ programming does not use pointers in complex ways.

What does it mean not to use pointers in complex ways?

(I really hope this is not a subjective question)

+7
c ++ pointers
source share
12 answers

As the guy who wrote this, I can at least tell you what I had in mind.

In a good C ++ program of the kinds that I am familiar with, pointers are used to indicate objects, mainly so that they can be passed and used polymorphically. They are not used for passing by reference, because links are used for this. There is no pointer arithmetic. There are not many source pointers. Pointers are not usually used to create data structures, since most of the data structures you need are built into the standard library or, possibly, Boost.

In other words, modern C ++ usually uses pointers in the same way as Java, except that Java does not use this word because it has no concept of anything other than the primitive data type available except for the pointer (by at least not then the last time I used Java). The translation comes from something like Foo bar = new Foo(); (syntax is not guaranteed) to smart_ptr<Foo> bar = new Foo; and from bar.snarf() to bar->snarf() . At least to get started, the Java programmer does not need to pick up a concept like him or her if he or she moves to C.

+7
source share

Of course, this is subjective. Some people seem to find almost all pointers “complex,” while some easily move between three (or more) levels of indirection when performing many arithmetic operations, never getting confused.

+13
source share

I suspect that this simply means that you have no raw pointers. Pointers should usually be held inside the class to which the pointer belongs and is responsible for deleting the object with the pointer (for example, std :: auto_ptr). This usually makes your code simpler.

+7
source share

My understanding of this answer is that “modern” C ++ does not need a lot of pointers, because most of the data structures that you would use are already built into either Boost or the standard library.

Thus, the “sophisticated” ways of using pointers would be to have pointers to pointers and traverse structures using pointers to really complex and potentially dangerous ways to use pointers, such as pointer math or worse.

I personally think that it depends on what code you write. There is a lot of code in which you need to write special data structures, so I would take this sentence with a lot of salt.

+6
source share

This statement makes no sense, since it is not possible to use C ++ pointers in a complicated way.

 #define NONCOMPLICATED * 

... Ducks and run away ...

+2
source share

if the indicated pointers

1) are used only as links to pieces of allocated memory blocks.

2) The specified pointer exists in only one place.

2.1) if the stack uses an exclusive smart pointer (regardless of the scope) (or is deleted manually at the end of the area).

2.2) if they belong to a class, correct compiled member functions are defined.

Than I would say, they are used in a reasonable and not complicated way: D

+1
source share

Pointers are not a problem. The statement is a version of a more general statement that the program should not complicate anything. The essence of program development is to eliminate complexity.

"Fools ignore complexity, pragmatists suffer from it, experts avoid it, geniuses remove it." (Alan Perlis)

"Any smart fool can make things bigger, more complex and more cruel. In order to move in the opposite direction, you need to show genius and courage." (Albert Einstein)

+1
source share

Beginning C ++ programmers or people coming from Java or C # usually use pointers everywhere.

Do you need an object like Foo? Foo* f = new Foo(); . Does the Bar class contain a member object? Make this a pointer to a dynamically allocated object.

And this is not the right way. The Foo object can be allocated on the stack. There is no need to use dynamic allocation, and there is no need to use a pointer. Bar can store a member object directly, not a pointer to it.

In good C ++ code, pointers should be rare. Not because deleting pointers alone makes your code better, but because heavy use of pointers is a sign that the person who wrote the code is pretending to be in Java or C #.

Other examples are the use of arrays and pointers, where std::vector would solve the problem. Or using pointers to implement pass by reference when actual references could be used.

+1
source share

Well, yes, of course, this is subjective - one person is “complex” - this is another “so simple that I can do it when we sleep.” For a slightly different bias, what about thinking about using pointers in general, and why this could be done. You can also think about maintainability of the code. The question from which you got the quote mentions hiring recent graduations - if the organization has a habit of hiring people who have no experience with pointers, then yes, in this organization good C ++ code will not have widespread use of pointers.

0
source share

Use pointers where you should, but nowhere else.

0
source share

This means that using pointers is, of course, obvious to the reader. What exactly implements depends on the given system.

For many people, they do not have the necessary experience and experience to solve complex problems at the memory level (debuggers, kernels, drivers, etc.). This domain requires an understanding of the pointer - or rather, an understanding of what is going on, which makes the decision of the pointer most obvious.

Most problems do not require solutions at the memory level; hence the use of Java, Haskell, C #, and other non-publishing languages.

0
source share

I would ask that it differ in the initial requirement. The problem is not complication, it is encapsulation versus communication. Good C ++ programs encapsulate pointers inside classes that have the sole purpose of managing pointers (pointers), maintaining invariants, and preventing the use of pointers that have become invalid (for example, by iterating from the end of the array). There can be a lot of heavy math pointer inside the class to provide linked lists, hash tables, comparisons and exchanges, transactional software memory using ACID semantics, etc., but all users are protected from such implementation details. And math pointer is not complicated by program logic, all this is done elsewhere.

0
source share

All Articles