Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Now that smart pointers exist, is it deprecated style to use C type pointers? [duplicate]

Possible Duplicate:
Which kind of pointer do I use when?

There are many pros in favour of C++11's smart pointers: They are safer, they're functionality and scope is more abvious etc.

Are the "classic" C like pointers

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

obsolete now? Are they even deprecated? Or are there use cases where C pointers still make sense?

edit: The code snippet with a smart pointer:

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

edit: Thanks for pointing out the duplicate. From Xeo's answer there:

Use dumb pointers (raw pointers) or references for non-owning references to resources and when >you know that the resource will outlive the referencing object / scope. Prefer references and >use raw pointers when you need either nullability or resettability.

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

like image 202
steffen Avatar asked Jul 23 '12 06:07

steffen


2 Answers

There are use cases where raw pointers make sense.

Raw pointers in modern code are 'non-owning' pointers. This means the code shouldn't do anything that requires or takes ownership of the pointed to object. For example it shouldn't be deleted, used to construct an owning smart pointer, or saved beyond the current context.

like image 66
bames53 Avatar answered Nov 15 '22 06:11

bames53


Absolutely not. The smart pointers are for special cases, not for general use. Depending on the application and programming style, most pointers will still be raw pointers; in some cases, in fact, there may be no smart pointers at all.

like image 27
James Kanze Avatar answered Nov 15 '22 08:11

James Kanze