📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page
  • auto_ptr
  • scoped_ptr
  • unique_ptr

Was this helpful?

  1. Chapter 10: Smart Pointers

Smart Pointers without Reference Counting

#include <memory>

C++ utilities library provides us three kinds of smart pointers without reference counting: auto_ptr, scoped_ptr and unique_ptr. These pointers have a common feature, that at the same time there can only be one pointer that manages the object. In other words, every time a new copy of a pointer transfer the ownership of objects from the previous one.

auto_ptr

auto_ptr rewrites the copy constructor so that every time a new copy of the pointer is created, it takes handle of its object, and the original pointer is set to NULL. In this way, there can only be one pointer pointing to the object. In this way, the destructor only need to deal with the only pointer pointing to the object. Therefore, we can not use the original pointer any more if its ownership has been transferred:

int main() {
    auto_ptr<int> p1(new int);
    auto_ptr<int> p2(p1);
    *p2 = 20;
    cout << *p1 << endl;    // ERROR
    return 0;
}

auto_ptr has to be used carefully, for programmers sometimes may not realize the transfer of ownership and invalidate the pointer unintentionally,.

scoped_ptr

scoped_ptr strictly regulates that there is always one pointer pointing to an object. It achieves this by disabling the copy constructor of operator=:

scoped_ptr(const scoped_ptr<T> &) = delete;
scpoed_ptr<T> &operator=(const scoped_ptr<T> &) = delete;

In this case, any copy of a scoped_ptr is not allowed:

int main() {
    scoped_ptr<int> p1(new int);
    scoped_ptr<int> p2(p1); // ERROR
    return 0;
}

Since scoped_ptr is to strict towards the ownership, it is not widely used.使用的比较少

unique_ptr

unique_ptr disables the copy constructor and operator= as well. But instead, it provides a copy constructor and operator= with Rvalue references:

unique_ptr(const unique_ptr<T> &) = delete;
unique_ptr<T> &operator=(const unique_ptr<T> &) = delete;
unique_ptr(unique_ptr<T> &&);
unique_ptr<T> &operator=(unique_ptr<T> &&);

In this case, we still cannot copy a pointer directly, but we can use the move semantics here:

int main() {
    unique_ptr<int> p1(new int);
    unique_ptr<int> p2(std::move(p1));
    return 0;
}

Now p1 transfer the ownership of the object to p2, and then be set to NULL. Again, the destructor only needs to deal with p2.

unique_ptr has almost the same functionality as auto_ptr, but is more recommended because its semantic is clearer. Under normal circumstances, copying a unique_ptr is not allowed, and we must explicitly use std::move. Hence, it will not happen that the programmer unintentionally transfer the ownership of a pointer, which is much safer.

PreviousSmart PointersNextSmart Pointers with Reference Counting

Last updated 4 years ago

Was this helpful?