Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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

There's even mutexes in there.



Creating 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.


Well 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?)


How 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: