Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Smart Pointers
#include <bits/stdc++.h> using namespace std; template <class myType> struct SmartPointer{ myType* ptr; SmartPointer(myType* x) : ptr(x){} ~SmartPointer(){ cout << "Smart Pointer Destructor ;P" << endl; delete ptr; } }; struct SmartPointerHelper{ vector<SmartPointer*> all; template <class myType> myType* get_new(){ myType* ptr = new myType(); all.push_back(new SmartPointer<myType>(ptr)); return ptr; } ~SmartPointerHelper(){ cout << "Starting to deallocate all dynamically allocated pointers..." << endl; // the vector is destroyed and in turn all the SmarPointers are destroyed correctly :) for(int j = (int)all.size()-1; j >= 0; j--) delete(all[j]); cout << "All the dynamic memory is deallocated successfully!" << endl; } }; SmartPointerHelper smp; struct node{ int x; node* ptr; node(int _x = 0) : x(_x){ cout << "node constructor called..." << endl; ptr = NULL; } ~node(){ cout << "node destructor called..." << endl; } }; int main(){ int *dynamic_int = smp.get_new<int>(); node* dynamic_node = smp.get_new<node>(); return 0; }
run
|
edit
|
history
|
help
0
Test 8(2020)
google
error
Hello world!
tree
t>0
FindMissingLagrange
k-tree 431 C
HashTable
ListTel