...
/Solution: Container Class with Multiple Destructors
Solution: Container Class with Multiple Destructors
Get an overview of how to write a Container class with multiple destructors.
We'll cover the following...
Solution
Press + to interact
#include <iostream>#include <string>#include <type_traits>template <typename T>class Container{T * t;int size;public:Container(int _size=0){size = _size;if(size>0)t = new T[size];elset = nullptr;}~Container() requires (std::is_trivially_destructible_v<T>) {std::cout << "trivial\n";delete [] t;}~Container() requires (!std::is_trivially_destructible_v<T>) {std::cout << "Not trivial\n";delete [] t;}};int main(){Container<int> * containerOfInts = new Container<int>(5);Container<std::string> * containerOfStrings = new Container<std::string>(10);Container<Container<float>> * containerOfContainers = new Container<Container<float>>(2);std::cout<<"destroying Container<int>: ";delete containerOfInts;std::cout<<"destroying Container<std::string>: ";delete containerOfStrings;std::cout<<"destroying Container<Container<float>>: ";delete containerOfContainers;}
On line 19 ...