How to Create a shared_ptr in C++? Last Updated : 15 Feb, 2024 Comments Improve Suggest changes Like Article Like Report A std::shared_pointer is a smart pointer introduced in C++11 that manages the lifetime of a dynamically allocated object through reference counting. In this article, we will learn how to create a shared_pointer. shared_ptr in C++A std::shared_pointer can be created by using the std::make_shared method which is defined in <memory> header in C++. Use the below syntax to create a shared_ptr. Syntax to Create shared_ptr in C++auto sharedPtr = make_shared<Type>(constructor_arguments);Here, Type is the type of the object that we want to allocate and manage with the std::shared_ptr.constructor_arguments are the arguments that are passed to the constructor of Type.C++ Program to Create shared_ptrThe below demonstrates how we can create a shared_ptr using the make_shared function and then access a member function of the class. C++ // C++ program to create shared_ptr #include <iostream> #include <memory> using namespace std; // defining my class class MyClass { public: // constructor of myClass MyClass() { cout << "MyClass Constructor\n"; } // destructor of myClass ~MyClass() { cout << "MyClass Destructor\n"; } void printHello() const { cout << "Hello!" << endl; } }; int main() { // Creating a shared_ptr using make_shared shared_ptr<MyClass> mySharedPtr = make_shared<MyClass>(); // Use the shared_ptr mySharedPtr->printHello(); // The MyClass instance will be automatically destroyed // when mySharedPtr goes out of scope return 0; } OutputMyClass Constructor Hello! MyClass Destructor Note: We can also create shared_ptr by directly using std::shared_ptr constructor. Comment More infoAdvertise with us Next Article How to Create a shared_ptr in C++? A ashish_rao_2373 Follow Improve Article Tags : C++ Programs C++ cpp-pointer CPP Examples Practice Tags : CPP Similar Reads How to Create a Thread in C++? A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t 2 min read How to Create a Smart Pointer in C++? A smart pointer in C++ simulates a pointer while also providing automatic garbage collection as it deallocates or frees associated memory when it goes out of scope, which helps prevent memory leaks and dangling pointers. In this article, we will learn how to create a smart pointer in C++. Creating a 4 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read How to Create a Pure Virtual Function in C++? In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To 2 min read How to Create Vectors of Unique Pointers in C++? In C++, the vector is defined as a dynamic array that can grow or shrink in size. Vectors of unique pointers are commonly used to manage the collections of dynamically allocated objects as they combine the dynamic resizing capability of vectors with the automatic memory management provided by unique 2 min read Like