From 6dd7eb7bc542dca2a3ad4826c2b9cbf4d08a19fe Mon Sep 17 00:00:00 2001 From: Butcher3Years Date: Sun, 8 Feb 2026 23:39:45 +0530 Subject: [PATCH] focuses on scopedptr class and its lifetimes --- .../Log.cpp | 54 +++++++++++++++++++ .../main.cpp | 49 +++++++++++++++++ this--keyword/main.cpp | 1 + 3 files changed, 104 insertions(+) create mode 100644 Object Lifetime-(stack $ scope lifetimes)/Log.cpp create mode 100644 Object Lifetime-(stack $ scope lifetimes)/main.cpp diff --git a/Object Lifetime-(stack $ scope lifetimes)/Log.cpp b/Object Lifetime-(stack $ scope lifetimes)/Log.cpp new file mode 100644 index 0000000..cf9b273 --- /dev/null +++ b/Object Lifetime-(stack $ scope lifetimes)/Log.cpp @@ -0,0 +1,54 @@ +#include +#include + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity!" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity!" << std::endl; + } +}; + +class ScopedPtr +{ + private : + Entity* m_Ptr; + public : + ScopedPtr(Entity* Ptr) + : m_Ptr(Ptr) + { + } + + ~ScopedPtr() + { + delete m_Ptr; + } + +}; + + + + +int main() +{ + + + { + + ScopedPtr e = new Entity();//we can also write it in constructor way and it is implicit conversion + Entity* e = new Entity(); //upper code allocated on heap and get destroyed after the scope ends ,scopedptr object allocated on stack! + } + + + std::cin.get(); +} + + +// we can write unq ptr which is a scoped ptr but now lets write in a simple class +// timer class for benchmarking and mutex locking[multiple threads cant access at sametime locks and unlocks after ] are examples of this type of using scopedptrs' diff --git a/Object Lifetime-(stack $ scope lifetimes)/main.cpp b/Object Lifetime-(stack $ scope lifetimes)/main.cpp new file mode 100644 index 0000000..19bca5a --- /dev/null +++ b/Object Lifetime-(stack $ scope lifetimes)/main.cpp @@ -0,0 +1,49 @@ +#include +#include + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity!" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity!" << std::endl; + } +}; + +int* CreateArray(int* array) +{ + + int* array = new int[50];//Heap allocation + //int array[50];//stack allocated + return array;//returning ptr in stack memory +} + +int main() +{ + +int array[50];//copying memory +int* a = CreateArray(array); + + { + //Entity e; + + Entity* e = new Entity(); + } + + + std::cin.get(); +} + + + +// {} THIS IS A SCOPE!! WE HAVE SCOPES FOR CLASSES,EMPTY CLASSES AND FOR IF STATEMENTS! +// JUST DEBUG THE CODE AND VERIFY EACH LINE +// WHILE STACK OR SCOPE ALLOCATION AFTER SCOPE ENDS IT DOES DESTROYS ENTITY +// WHILE HEAP ALLOCATION IT DOES LIVES EVEN AFTER SCOPE ENDS +// BY USING "Scoped ptr" OR SCOPE CLASSES AND UNQ PTR WHICH IS A SMART POINTER WE CAN ALLOCATE ON HEAP AND AUTO DELETE THE MEMORY AFTER SCOPE ENDS +// A CLASS THATS A WRAPPER OVER A POINTER WHICH UPON CONSTRUCTION HEAP ALLOCATES THE POINTER AND THEN UPON DESTRUCTION DELETES THE POINTER \ No newline at end of file diff --git a/this--keyword/main.cpp b/this--keyword/main.cpp index fb91c9f..a564cf0 100644 --- a/this--keyword/main.cpp +++ b/this--keyword/main.cpp @@ -38,3 +38,4 @@ int main() } +//IN Const methods this is a const entity constant pointer!! \ No newline at end of file