Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Automate Resource Management with shared_ptr-4 : Page 4




Application Security Testing: An Integral Part of DevOps

Pimpl Revisited
shared_ptr can eliminate the hassles of manual memory management when implementing the Pimpl idiom, which I presented here last month. In the header file containing the class' declaration, replace the opaque pointer member with a shared_ptr (the revised code is highlighted):

//++file string.h #include <iosfwd> #include <memory>//for shared_ptr class String { public: String (); ~String(); //... size_t length() const; ostream & operator << (ostream& s) const; //... private: struct StringImpl; //fwd declaration of internal struct shared_ptr <StringImpl> pimpl; //instead of opaque ptr }; In the .cpp file, remove the destructor: //++file string.cpp #include <vector> #include "Lock.h" #include "String.h" struct String::StringImpl { vector <char> vc; size_t len; Lock lck; }; String::String(): pimpl (new String::StringImpl) {}

That's all!

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date