Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Memory
  2. Pointers
  3. Smart Pointers

Weak Pointer

Weak pointers in C++ are smart pointers that hold a non-owning ("weak") reference to an object that is managed by a shared pointer. They are used to break circular references between shared pointers and to observe an object without affecting its lifetime. Unlike shared pointers, weak pointers don't increment the reference count of the object they point to.

Here's a simple example to demonstrate the use of weak pointers:

#include <iostream>
#include <memory>

class Person {
public:
    Person(const std::string& name) : name_(name) {
        std::cout << name_ << " created" << std::endl;
    }
    ~Person() {
        std::cout << name_ << " destroyed" << std::endl;
    }
    void greet() {
        std::cout << "Hello, I'm " << name_ << std::endl;
    }

private:
    std::string name_;
};

int main() {
    // Create a shared pointer
    std::shared_ptr<Person> shared = std::make_shared<Person>("Alice");
    
    // Create a weak pointer from the shared pointer
    std::weak_ptr<Person> weak = shared;
    
    // Use the weak pointer
    if (auto temp = weak.lock()) {
        temp->greet();
    } else {
        std::cout << "Object no longer exists" << std::endl;
    }
    
    // Reset the shared pointer ref counter
    // i.e. release all weak pointers references
    shared.reset();
    
    // Try to use the weak pointer again
    if (auto temp = weak.lock()) {
        temp->greet();
    } else {
        std::cout << "Object no longer exists" << std::endl;
    }
    
    return 0;
}

Key points in this example:

  1. We define a simple Person class with a constructor, destructor, and a greet() method.

  2. In main(), we create a shared_ptr<Person> named shared.

  3. We then create a weak_ptr<Person> named weak from the shared pointer.

  4. To use the weak pointer, we need to call its lock() method, which returns a shared_ptr. If the object still exists, we can use it; otherwise, lock() returns a null pointer.

  5. After resetting the shared pointer, the Person object is destroyed because there are no more shared_ptrs pointing to it.

  6. When we try to use the weak pointer again, lock() returns a null pointer, showing that the object no longer exists.

PreviousShared PointerNextReference count

Last updated 9 months ago

Run it .

🎯
here