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 How to Implement User Defined Shared Pointers in C++? shared_ptr is one of the smart pointer introduced as a wrapper to the old raw pointers in C++ to help in avoiding the risks and errors of raw pointers. In this article, we will learn how to implement our own user defined shared pointer in C++. What is shared_ptr in C++? A std::shared_ptr is a contai 5 min read How to Access Elements of a Pair in C++? In C++, a pair container is defined in <utility> header that can store two values that may be of different data types so as to store two heterogeneous objects as a single unit. In this article, we will learn how to access elements of a pair in C++. Example:Input: myPair={10,G} Output: First El 2 min read How to use const with Pointers in C++? In C++, the const keyword is used as a type qualifier for defining read-only (immutable) objects that cannot be modified anywhere in their lifetime. It can be used in several ways with pointers, each serving a different purpose. In this article, we will learn how to use const qualifier with pointers 3 min read How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will 2 min read How to Create Custom Memory Allocator in C++? In C++ containers, memory allocation is generally managed by allocators through new and delete operators or malloc() and free() functions but for custom memory management we can use the custom allocators. In this article, we will learn how can one create a custom memory allocator in C++. Custom Memo 2 min read Like