(C++) Answer of exercise \#10: Obtaining a read-only (smart?) pointer
February 24, 2017 · View on GitHub
(C++)
Answer of exercise #10: Obtaining a read-only (smart?) pointer
This is the answer of Exercise #10: Obtaining a read-only (smart?) pointer.
#include <boost/checked_delete.hpp> #include <boost/shared_ptr.hpp> struct MyStruct { int m_x; private: ~MyStruct() {} friend void boost::checked_delete<>(MyStruct *); }; struct MyStructKeeper { MyStructKeeper() : m_s(new MyStruct) {} const MyStruct * Get() const { return m_s.get(); } private: boost::shared_ptr<MyStruct> m_s; }; int main() { MyStructKeeper k; const MyStruct * s = k.Get(); //Should be required to write 'const MyStruct' const int x = s->m_x; //Must compile //s->m_x = 0; //Should not compile //delete s; //Should not compile }