Posts

Showing posts from 2022

Smart Pointers in C++ and How to Use Them

Image
  Smart Pointers in C++ and How to Use Them In this article, we will be discussing smart pointers in C++. What are Smart Pointers, why, and how to use them properly? Pointers are used for accessing the resources which are external to the program – like heap memory. So, for accessing the heap memory (if anything is created inside heap memory), pointers are used. When accessing any external resource we just use a copy of the resource. If we make any change to it, we just change it in the copied version. But, if we use a pointer to the resource, we’ll be able to change the original resource. Play Video Click here for the Complete Course! Problems with Normal Pointers Take a look at the code below. C++ #include <iostream> using namespace std;   class Rectangle { private :      int length;      int breadth; };   void fun() {      // By taking a pointer p and      // dynamically creating object      // of class rectangle      Rectangle* p = new Rectangle(); }   int main() {