tag:blogger.com,1999:blog-6314876008291942531.post6934652678665785314..comments2024-01-14T00:36:43.430-08:00Comments on Antonio Gulli's coding playground: Design a smart pointer in C++Unknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-6314876008291942531.post-40493953316727266692012-01-16T08:30:37.838-08:002012-01-16T08:30:37.838-08:00Worth to read documentation on Qt blogs about Qt s...Worth to read documentation on Qt blogs about Qt smart pointers (note that they were timely evolved),<br /><br />http://labs.qt.nokia.com/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have/Anonymoushttps://www.blogger.com/profile/09855100606552890649noreply@blogger.comtag:blogger.com,1999:blog-6314876008291942531.post-13000705898163492262011-12-14T15:32:17.453-08:002011-12-14T15:32:17.453-08:00Let's forget the thread safety, and let's ...Let's forget the thread safety, and let's make it sharable, no copy on write, and it doesn't point to a vector;<br /><br />template ;<br />struct Storage {<br /> Storage(T* aPointer)<br /> : thePointer(aPointer),<br /> theCounter(1)<br /> {}<br /><br /> ~Storage() {<br /> assert(theCounter == 0);<br /> delete thePointer;<br /> }<br /><br /> T* thePointer;<br /> size_t theCounter;<br />}<br /><br />template <br />class SmartPointer {<br /> typedef SmartPointer THIS;<br /> public:<br /> SmartPointer(T* aPointer)<br /> :theStorage(new Storage(aPointer))<br /> {} <br /> <br /> //copy constructor<br /> SmartPointer(const THIS& aSM)<br /> :theStorage(aSM.theStorage) {<br /> ++(theStorage->counter);<br /> }<br /><br /> //assignment operator<br /> SmartPointer& operator=(const THIS& aSM) {<br /> if (this == &aSm) return *this;<br /> if (--(theStorage->counter) == 0) {<br /> delete theStorage;<br /> }<br /> theStorage = aSm.theStorage;<br /> ++(theStorage->counter);<br /> }<br /><br /> ~SmartPointer() {<br /> if (--(theStorage->counter) == 0) {<br /> delete theStorage;<br /> }<br /> }<br /><br /> const T* operator->() const {<br /> return theStorage->thePointer;<br /> }<br /><br /> T* operator->() {<br /> return theStorage->thePointer;<br /> }<br /><br /> const T& operator*() const {<br /> return *(theStorage->thePointer)<br /> }<br /><br /> T& operator*() {<br /> return *(theStorage->thePointer)<br /> }<br /> private:<br /> Storage* theStorage;<br />};Gaetanohttps://www.blogger.com/profile/03637292910769902384noreply@blogger.comtag:blogger.com,1999:blog-6314876008291942531.post-54760080170480960482011-12-13T17:24:30.952-08:002011-12-13T17:24:30.952-08:00valid questions to start an interview. Make your a...valid questions to start an interview. Make your assumptions and start coding ;)codingplaygroundhttps://www.blogger.com/profile/08478993186814330588noreply@blogger.comtag:blogger.com,1999:blog-6314876008291942531.post-55577780432621345302011-12-12T14:18:34.182-08:002011-12-12T14:18:34.182-08:00Overloaded question.
Smart respect to what? Can ...Overloaded question. <br /><br />Smart respect to what? Can be shared? With a movable constructor? Thread safe? It should be able to point to a vector?Gaetanohttps://www.blogger.com/profile/03637292910769902384noreply@blogger.com