Tuesday, March 22, 2011

C++:auto_ptr, scoped_ptr, shared_ptr and weak_ptr

簡單整理各種Smart Pointer的特色:
  • auto_ptr
    • Supported in C++ Standard Library
    • Support Ownership Transfer
  • scoped_ptr
    • Supported in C++ TR1
    • Not Support Ownership Transfer
  • shared_ptr
    • Supported in C++ TR1
    • Using Reference Count
    • Object copy or assignment will increase the reference count.
  • weak_ptr
    • Supported in C++ TR1
    • Created from shared_ptr. Cannot be created alone.
    • Support Ownership Transfer
    • Easy and safe to check a pointer valid or not, without increasing the reference count.
延伸閱讀: