Tag Info

New answers tagged

1

Java References and C pointers differ in exactly two points: There's no pointer-arithmetic for the former. And you cannot create a Java reference to whatever you want, you can only copy those saved somewhere accessible (static fields, fields of objects, local variables) or returned by function-invocations (like constructor-calls), which thus all refer to ...


0

They are slightly different. In Java a copy of the reference is copied to the stack of a called function, pointing to the same object as the calling function and allowing you to manipulate that object. However you cannot change the object the calling function refers to. Consider the following java code public static void changeRValue(StringBuffer sb){ ...


2

Note: This answer addresses the final part of the question. The bulk of the question has already been nicely addressed in Niall's answer. I am particularly worried about the getData implementation; whether I should return a copy of the shared pointer or a naked pointer and why. Suppose some code calls getData() and saves that pointer in some ...


3

Just use shared_ptr directly, with your custom deleter. Maybe typedef it if you prefer. This way you get correct move & copy constructors and assignment operators with no typing. You also get weak_ptr for free, if you want it. Unless your code will add some actual functionality - or at least an interface compatible with some external requirement you ...


4

I think these implementations are reasonable and a generally good solution. Adding an appropriate move constructor and move assignment may help deal with your copy concerns - the default should be appropriate with the shared wrapper. Some may argue (or advise) that you do not need to wrap the Standard Library facilities that you use here; whilst this is ...


2

No, they are not avoided per se. Use "raw" pointers as appropriate, with a long list of things where they are not appropriate. To keep this short: Do not use raw pointers to model ownership (avoid naked deleteand possibly also naked new -> make_sharedor make_uniqueare your friends) Do not use raw pointers to "pass around" a conceptual non-owning ...


8

As humans, we are fallible and prone to making errors. Therefore, we should try to write our programs as safe as possible, and let tools like the compiler or Valgrind help us find problems before they lead to crashes in production. It is clear that it is impossible to avoid pointers at all levels – they provide a mechanism for indirection that is invaluable ...


11

If you are starting a new green field project that is at a high enough level (as in you're talking about writing an enterprise Foo rather than a device driver) and has no external dependencies you can probably avoid using pointers entirely. But most development isn't brand new development, it's leveraging the millions of existing lines of C++ code out there ...



Top 50 recent answers are included