Why would you pass a smart pointer as an argument to a function in C ++?

I am very new to C ++ smart pointers, and it's hard for me to understand this advice using them for function arguments.

"C ++ Coding Standards: 101 Rules, Recommendations, and Recommendations," says these are reasons for moving into a smart pointer.

Prefer gear pointer (smart)

  • if the argument is optional (therefore, callers may pass null as "inaccessible" or "do not care")
  • or if the function saves a copy of the pointer
  • or else manipulates the ownership of the argument.

Can someone please give me examples of each of them, and why not use a smart pointer in bad cases?

+4
source share
4 answers

Prefer transmission pointer (smart) [when ...]

I think you are reading the coding standard incorrectly. You read it as "these are the reasons for using a smart pointer." What the author intended is "these are reasons to use some kind of pointer, which may be an optional smart pointer."

+7
source

Prefer gear pointer (smart)

Notice how smart is in brackets?

What a person is talking about goes through a pointer against a link. Smart is in parentheses because you will follow similar rules when working in a team that prefers smart pointers.

+4
source

I am going to suggest that this question refers to pointers in general, as this explains the quoted text in the question.

There are several reasons why you would like to use a smart pointer (or the good old-fashioned pointers in general). The first and most obvious is that when you pass something by reference or pointer, you pass a pointer to an element in memory, which means that you get the actual object instead of the copy, as you get when passing by value. This is useful when you want to manipulate an object with a function or just cut back on copy (imagine you send a large text file as a value all the time, which would be inefficient!)

Next, the ability to pass something as a null value. This basically means that the parameter can be passed as "does not exist", which, in turn, can be processed in logic. For example, if the file pointer is NULL, a new file is created.

For smart pointers, in particular: Smart pointers are pointers that have additional control algorithms that go backstage, it can be reference counting or other parameters. For example, you can use unique_pointer to make sure that at the moment there is only one pointer to an object. For more information see Wikipedia http://en.wikipedia.org/wiki/Smart_pointer

If the question is really related to the work of shared pointers, see also this introduction http://www.tech-recipes.com/rx/1232/c-pointers-pass-by-value-pass-by-reference/

+2
source

Smart pointers exist for one and only one function:

argument ownership

Smart pointers provide this so that you don’t hack it, which is very likely, since manual control of ownership is ridiculously error prone, while smart pointers guarantee.

There is no other excuse for using a smart pointer over a regular pointer.

+1
source

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


All Articles