Ö
Öö Tiib
This seems to assume that pointers are used exclusively for handling
dynamically allocated memory.
I did not tell what is used. Used are awful idioms. Just download
random C++ source from net and see. I did suggest rules how RAII can
be taken into usage in C++ very simply.
That's most certainly not so. Remember that pointers are, effectively,
iterators, and there are situations where you can (and should) use
pointers as iterators (eg. when calling or even implementing an
iterator-based algorithm.)
Can you give example where I must use raw pointers instead of full
iterators? Feels like some sort of preliminary optimization. On cases
that I know of the real iterators perform as well as raw pointers
while being more type-safe, having less operators and better
debug-time checks. So ... I need example.
There are also other (although rarer) cases where you may want pointers
that are not used as iterators, pointing to some non-dynamically allocated
thing.
Why? Storing such feels error-prone. Passing as argument feels pointless:
If it is not nullptr use reference, if it is nullptr use overload without
that parameter. Interfacing with module written in C ... I mentioned.
'new' is perfectly ok if you are implementing a class that manages its
own resources (as long as you use 'new' only inside that class and
strictly follow the "rule of three".)
Why to use new if you have nothing to initialize with it?
There are no raw pointers. Your complaint was that there may be
iterators and pointers to non-dynamic data. I disagreed on those too
but ... Do you want to initialize iterator with new? Do you want to
initialize pointer that can point at non-dynamic data with new?
Except if you are implementing such a class.
Same reason. It is preliminary optimization at best and usually just
pointless. The smart pointers mop up with zero code typed. It is
'reset()' not 'delete' with those if you need to do it explicitly
ahead of time. Some more exotic resources may need to close(), stop()
and/or terminate() ... but there's nothing to 'delete'.