Module 5
Advanced Smart Pointers & Modern C++ Features
30 mins read
Lesson 1: Smart Pointers
1.1 `unique_ptr` vs `shared_ptr`
Understanding ownership semantics, reference counting with `std::shared_ptr`, and avoiding circular references with `std::weak_ptr`.
C++
#include <iostream>
#include <memory>
class MyClass {
public:
void display() { std::cout << "Hello Smart Pointers!" << std::endl; }
};
int main() {
std::shared_ptr<MyClass> sharedPtr = std::make_shared<MyClass>();
sharedPtr->display();
return 0;
}