-1

Sometimes it's convenient to split interface of some system/library in more than one class.

For example, consider idea of library for playing Chess. Its interface would use (and deliver to players) different object for every single game and - during game - another object for every figure.

In Java there wouldn't be such a problem. But in C++, a library user can delete (or make attempt to delete) every pointer he'll get. Even shared_ptr/weak_ptr.

What do you think about such situations? Should I use in my interface wrapping classes that deleting isn't dangerous?

What is an usual way for such dilemmas?

Is there a way that STL smart pointers would help? I heard that they should be used always and only to express ownership, so they seem to have nothing to do with this issue (Chess is owner of SingleGame, SingleGame is owner of every Figure).

PS did I use correct tags/subject?

Krzysztof Stanisławek
  • 1,267
  • 4
  • 13
  • 27
  • what?? I really didn't understand the problem.. if a programmer can't use C++ you can't save him from eternal memory damnation – Marco A. Jul 02 '14 at 20:37
  • Simple rule is owner deletes. So if you track ownership with a smart pointer you can track who is responsible for deleting it. – user1937198 Jul 02 '14 at 20:38
  • A user that deletes a pointer got via a smart pointer (e.g. `std::shared_ptr`) deserves to get leftover with debugging his crappy code himself. – πάντα ῥεῖ Jul 02 '14 at 20:39
  • So I shouldn't be worried that somebody will destroy some object too soon at all? I'm asking about good practices. I don't want to write crappy code. – Krzysztof Stanisławek Jul 02 '14 at 20:44
  • Yes, it's good practice **not** to pass around raw pointers, use [smart pointers](http://en.cppreference.com/w/cpp/memory) instead. – πάντα ῥεῖ Jul 02 '14 at 21:08
  • @πάνταῥεῖ http://stackoverflow.com/questions/23408810/smart-pointers-cycles Do you disagree with the sentence: "Approach this from the conceptual side, not the implementation one. Smart pointers represent ownership. And existence of smart pointers does not invalidate the role of raw pointers as non-owning observers." ? – Krzysztof Stanisławek Jul 02 '14 at 21:15

2 Answers2

1

You can't stop a user from breaking stuff. As others have suggested, use smart pointers. With C++11, there is no reason not to use them in new code. If the user still breaks it, that's their fault. You can't design a library that is completely foolproof. You can just do your best to disuade foolish behavior.

jaredready
  • 2,398
  • 4
  • 23
  • 50
  • I know, user can always write something like `int k; delete &k;`, but I talk about something that we can consider as part of public interface. It was on my mind lastly I wanted to know clearly how programmers look at such issues. Now I know that it is not very important. Although, I found something that should help somebody pedantic: http://stackoverflow.com/questions/12536280/private-operator-delete – Krzysztof Stanisławek Jul 02 '14 at 21:44
1

As others have said, smart pointers (or other RAII schemes) are often a great idea. They can clearly indicate ownership and at the same time provide an automatic mechanism for managing it. Try using such if you can.

But really, no reasonable C++ programmer should be blindly calling delete on every pointer they get. When they use a library/API/whatever which returns a pointer/handle/resource/etc they should be reading its documentation to tell them whether or not they will be responsible for deallocation and if so then when technique should be used.

So at a minimum, just make sure your public interface clearly indicates when ownership is passed to the caller and what method they should use for cleanup.

TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17