fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class base
  7. {
  8. public:
  9. virtual ~base(){}
  10. };
  11.  
  12. template<typename T>
  13. class derived : public base {
  14. public:
  15. derived(const T t_){}
  16. };
  17.  
  18. class myClass
  19. {
  20. public:
  21. template<typename T>
  22. myClass(const T t) : data(new derived<T>(t)){}
  23. ~myClass(){delete data;}
  24. private:
  25. base* data;
  26. };
  27.  
  28. int main() {
  29.  
  30.  
  31. vector<myClass> vec;
  32.  
  33. myClass obj(22);
  34.  
  35. vec.push_back(obj); // constructor is invoked and then destructor is invoked, why?
  36.  
  37.  
  38. return 0;
  39. }
Runtime error #stdin #stdout 0s 3424KB
stdin
Standard input is empty
stdout
Standard output is empty