samedi 26 septembre 2020

working of universal initialization causing destructor to be called one extra time


        #include<iostream>
        #include<vector>

        class test {
            public:
                  virtual ~test() 
                  {
                      std::cout<<"inside destructor \n";
                  }
    
        };
    


        int main()
        {
            std::cout<<"---with universal initializer--------- \n";
            {
             test t1;
             std::vector<test> v{t1};
            }
            std::cout<<"----- ending universal initailizer ----- \n";
    
            std::cout<<"------------------------\n ";
            test t2;
            std::vector<test> v2(1,t2);
    

            return 0;
         }

output:

---with universal initializer--------- inside destructor inside destructor inside destructor ----- ending universal initailizer -----

inside destructor
inside destructor

my question is why destructor is called one extra time or extra object is created compared to other initialization when initialization done using universal initialization

Aucun commentaire:

Enregistrer un commentaire