3 ms·
How is abstraction unsafe? Additionally, raw pointers are not dangerous. If you're passing them around as const pointers, the receiver can't do things like del
by 72deluxe 12y ago
How is abstraction unsafe?
Additionally, raw pointers are not dangerous. If you're passing them around as const pointers, the receiver can't do things like deleting them. If you are writing a container class (instead of using one of the existing myriad of containers in the STL) then you can use raw pointers.
But typically you would not need to write your own container class; just use one of the STL's and the move semantics. You can then use references everywhere instead. Or const references. Everyone forgets about const correctness in their C++ bashing.
For concurrency, you may wish to see http://en.cppreference.com/w/cpp/thread http://en.cppreference.com/w/cpp/thread
There's even mutexes in there.
- Jweb_Guru 12y agoCreating a dangling const pointer is pretty trivial. Hell, make it a const reference. #include <iostream> struct Foo { const int &dangling; Foo(const int &dangling): dangling(dangling) { } }; Foo foo() { int dangling = 0; Foo foo(dangling); return foo; } int main() { std::cout << foo().dangling << std::endl; return 0; } Neither g++ nor clang warn on this, either. I am not sure why you think const pointers are safe. They aren't. Flat out.
- 72deluxe 12y agoWell I was assuming for pointers that they'd be initialised to something sensible. That would be a coding standard problem if they're not. The example you give is also a problem with coding standards, I'd argue. It isn't the languages fault that you're using a reference that's going out of scope - that's just bad coding. (A pointer going out of scope isn't so bad, no?)
- Animats 12y agoHow is abstraction (hiding) unsafe? Objects hide their implementation details. An object is only a valid abstraction if it correctly hides its implementation details and the user can ignore them. If there are hidden constraints on what a user can do with an object, but those are not enforced by the object, the object is an unsuccessful abstraction and a potential source of bugs. Additionally, raw pointers are not dangerous. Two words: "buffer overflow". C pointers lose size information.