Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Examples

Example: Using a factory function to insert a post-constructor call. Consider:

class B {                                         // hierarchy root
protected:
 B() { /* ... */ }

 virtual void PostInitialize() { /* ... */ }      // called right after construction

public:
 template<class T>
 static shared_ptr<T> Create() {                  // interface for creating objects
  shared_ptr<T> p( new T );
  p->PostInitialize();
  return p;
 }
};

class D : public B { /* ... */ };                 // some derived class

shared_ptr<D> p = D::Create<D>();                 // creating a D object

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial