diff --git a/Smart pointers -- Types/main.cpp b/Smart pointers -- Types/main.cpp new file mode 100644 index 0000000..3d9ff32 --- /dev/null +++ b/Smart pointers -- Types/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity" << std::endl; + } + + void Print() {} + +}; + +int main() +{ + { + //std::unique_ptr entity(new Entity()); + + std::unique_ptr entity = std::make_unique(); //prefferd to call make unique to avoid constructor exceptions[ptr with no ref and thats a memory leak]! + + //std::unique_ptr e0 = entity; copy constructor and assignment operator actually deleted! + + entity->Print(); //EXACTLY SAME AS RAW POINTER + } + + + std::cin.get(); +} + + + +// SMART POINTERS ARE TO AUTO DELETE THE MEMORY ADDRESS THAT IS HEAP ALLOCATED!! +// AND SOME ARE NOT GONNA MENTION NEW AND ALSO DELETE KEYWORDS!! +// SMART POINTERS ARE THE WRAPPER AROUND A REAL RAW POINTER +// MEMORY AUTO DELETES ACCORDING TO THE POINTER YOU USED +// UNQ POINTER IS A SCOPED PTR[YOU CANT COPY UNQ PTR] OR TWO UNQ PTRS POINTING TO SAME BLOCK OF MEMORY AND ONE OF THEM DIES IT FREES THE MEMORY +// THIS IS THE ONLY PTR AND THAT ONLY REF TO THAT PTR YOU ACTUALLY WANTS +// WAY OF WRITTING IS UNQ PTR HAS EXPLICIT CONSTRUCTOR AND FIRST WITH CLASS NAME AND GIVING A OBJECT NAME AND KEEPING IT A CONSTRUCTOR \ No newline at end of file diff --git a/Smart pointers -- Types/shared.cpp b/Smart pointers -- Types/shared.cpp new file mode 100644 index 0000000..c0b0a7d --- /dev/null +++ b/Smart pointers -- Types/shared.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity" << std::endl; + } + + void Print() {} + +}; + +int main() +{ + { + { + std::shared_ptr e0; + { + + + std::shared_ptr sharedEntity = std::make_shared(); + e0 = sharedEntity; + // std::shared_ptr e0 = std::make_shared(); + /*Here this is preferrable due to this shared ptr stores memory in control block + so if you do allocation it allocate first new Entity and then construct the shared + ptr in control block which is 2 allocations*/ + + } //not yet destroyed + } + } + + + std::cin.get();//after this memory frees after all shared ptr dies +} +//shared ptr based on ref counting +//when shared ptr gives to other shared ptr countis 2 +//when all ref summation is 0 the memory frees + diff --git a/Smart pointers -- Types/weak.cpp b/Smart pointers -- Types/weak.cpp new file mode 100644 index 0000000..b7e4e92 --- /dev/null +++ b/Smart pointers -- Types/weak.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + + +class Entity +{ + public : + Entity() + { + std::cout << "Created Entity" << std::endl; + } + + ~Entity() + { + std::cout << "Destroyed Entity" << std::endl; + } + + void Print() {} + +}; + +int main() +{ + { + { + std::weak_ptr e0; + { + std::shared_ptr sharedEntity = std::make_shared(); + // std::weak_ptr weakEntity = sharedEntity;//copy Entity but not increase the ref counting + e0 = sharedEntity; + + } + }//DESTROYED ENTITY + } + + + std::cin.get(); +} + +//shared ptr to shared ptr ref count increases +//shared ptr to weak ptr ref count remain same like you have Entities but you dont take ownership[it wont keep you alive] +//try to use unq ptr and then shared always \ No newline at end of file