D
D
Daniil Demidko2016-03-09 08:31:27
C++ / C#
Daniil Demidko, 2016-03-09 08:31:27

Smart pointers?

Always in pluses tried to replace pointers with const T& or &T.
The only place where they cannot be replaced is when returning a raw array from a function, but you can always use std::vector here.
I began to study C ++ 11, C ++ 14 in more detail according to Meyers' book "Efficient and Modern C ++", smart pointers are being pushed there, as I understand wrapper classes over raw ones, with their own behavior. So I figured out how to use them. But where can they be used?
Why not keep using T& and const T& along with std::vector ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
Xano, 2016-03-09
@Daniro_San

If you don't know why you need smart pointers, you don't need them.
Probably their most important property is the guarantee that the object will not be destroyed as long as there is a smart pointer + the possibility of destroying the object in an arbitrary place, when no one needs it.
One example where smart pointers are justified is using multiple threads to perform operations on a single object.

S
Stanislav Makarov, 2016-03-09
@Nipheris

You may not have mastered the OO features of C++ yet. Then your question is more than logical.
You see, in a language where there is any OO model, there will inevitably be two categories of data types - data types whose instances behave like values ​​(values), and data types whose instances behave like variables / objects (if it is not clear, I will describe in more detail).
A distinctive feature of an object from a theoretical point of view is identity - a certain key or feature with which this object is uniquely identified. In OO databases, the so-called identity is used as an identity. ID. C++ uses pointers as identity objects. This is convenient, because the place of an object in memory uniquely identifies it (here one can trace the fact that objects in C ++ are the essence of variables, the state of which can only be changed in a strictly defined way).
Although the C++ language allows in theory to work with any type both "by value" and through pointers, real classes are usually designed to emphasize their semantics .value/object. For example, for types that behave like "values", a copy constructor is written and assignment operations are allowed. For "object" types, on the contrary, the assignment operation and the copy constructor are prohibited. Of course, nothing prevents us from turning a pointer into a reference and working by reference, but using a reference "hides" information about the identity of an object from us. For example, to compare the identity of two objects (if they are exactly "objects" and not "values"), it is enough to compare only their identity, in terms of C ++ - pointers. If you turn them into references, you will have to use the address operation: &a == &b, which is less logical and convenient than comparing ordinary pointers: a == b. In addition, an important difference of the pointer is that
However, while regular pointers are a convenient way to model the concept of identity, they don't provide any help for managing the life cycle of dynamic variables (which most objects are). As a result, it is much less convenient for the programmer to use dynamic variables than automatic ones. And in order to kill two birds with one stone - to have dynamic object variables with a flexible lifetime and a unique identity, and manage them in a convenient way - they came up with smart pointers. Essentially, these pointers tie the lifetime of a dynamic variable in one way or another to the structure of the program and to the places where those variables are used by implementing the concepts of variable ownership.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question