Pimpl idiom used with class member variable

What is the correct way to implement this class?

//Header #include <boost/shared_ptr.hh> class MyClass { public: static foo() static foobar(); private: class pimpl; static boost::shared_ptr<pimpl> m_handle; static bool initialized; }; //source namespace { bool init() { //... // init() can't access m_handle, unless it is a friend of MyClass // but that seems a bit "tacky", is there a better way? } } class MyClass::pimpl { public: ~pimpl(){} } bool MyClass::initialized = init(); MyClass::foo() { //... } MyClass::foobar() { //... } 
+4
source share
1 answer

MyClass is singleton - some call it Illustrious Global. An often abusive pattern. Use private ctors and a public static accessor:

  MyClass { public: static MyClass& Instance() { static MyClass obj; return obj; } // ... private: MyClass() : m_handle(pimpl()), initialized(true) {} // ... }; 
+4
source

Source: https://habr.com/ru/post/1302180/


All Articles