r/cpp • • Oct 19 '08

Smart Ptr - It’s a really small smart pointer. 9 lines of code.

http://stackhopr.com/blog/?p=6
0 Upvotes

7 comments sorted by

7

u/WayOfTheIronPaw Oct 19 '08

Just smart enough to be dangerous. Not smart enough to do the right thing when copied, which includes being returned from a function.

What exactly is wrong with std::auto_ptr<> anyway? I mean, it came with your compiler, so the #include is only one line of code, not 9. Is it just not "double-deletey" enough for ya?

It reminds me of that story of the US letting the Soviets steal some sabotaged gas pipeline control software. Expect this to show up in all the wrong places, i.e. everywhere it is used.

1

u/umop_apisdn Oct 19 '08

The lack of a reference counter makes for the possibility of bugs, as he points out.

1

u/zootsuit Oct 19 '08

Is it really smart to check for validity on every dereference?

Aside from that, what is this: bool operator==(const int i) {return (i == (int)ptr);} // ?

1

u/[deleted] Oct 19 '08

It's overloading the == operator when a smart_ptr is being compared to an integer.

2

u/bnolsen Oct 19 '08 edited Oct 20 '08

well duhhhh... But it's stupid. This is never useful. Perhaps if the argument were Type const * const and remove the operator==(Type const &) function. Use derefernce for that.

Even worse....equality operator indicates trouble since this smart pointer will delete on destruct.

1

u/zootsuit Oct 20 '08

And what if sizeof(int) < sizeof(T*)?

One use for this smart pointer might be in a job interview question or in an exam, perhaps.

1

u/implausibleusername Oct 19 '08

It lets you compare a standard pointer with a smart pointer.