Now that smart pointers exist, is the style out of date for using type C pointers?

Possible duplicate:
What type of pointer do I use when?

There are many advantages to using C ++ 11 smart pointers: they are safer, their functionality and scope are more obvious, etc.

Are the "classic" C-oriented pointers

class C{}; C c; C* c_p = &c; 

out of date? Are they even out of date? Or are there cases where C pointers still make sense?

edit: Code snippet with smart pointer:

 class C{}; C c; std::shared_ptr<C> c_p(new C()); 

edit: Thanks for pointing out the duplicate. From Xeo, answer:

Use dumb pointers (raw pointers) or links to refer to non-resource resources, and when> you know that the resource will go to the link object / scope. Prefer links and> use raw pointers if you need null or returnability.

If that's all there is, I accept that this question has been closed.

+6
source share
3 answers

There are cases where the original pointers make sense.

The source pointers in the modern code are "no property rights" pointers. This means that the code should not do anything that requires or takes responsibility for the specified object. For example, it should not be delete d, used to create a smart pointer for the owner, or stored outside the current context.

+13
source

Absolutely not. Smart pointers are for special occasions and not for normal use. Depending on the style of application and programming, most pointers will still be raw pointers; in some cases, in fact, should not be smart pointers.

+9
source

No, raw pointers are not out of date. Their use is usually discouraged, if necessary. Retaining ownership of an object using an unprocessed pointer is discouraged even more, because you must remember to delete it, which can be difficult if there are exceptions.

Raw pointers are somewhat more efficient than smart pointers, so using them makes sense in some parts of the code that are performance-sensitive. In some areas, such as linear algebra, pointer arithmetic can be useful, and you should use the source pointers there [1] or create new abstractions on top of the source pointers, perhaps in combination with smart pointers, and not just use smart pointers.

[1]. to determine the type of sub-matrix

+4
source

Source: https://habr.com/ru/post/921103/


All Articles