Why is the destructor of the class called twice ?

Posted by dicaprio on Stack Overflow See other posts from Stack Overflow or by dicaprio
Published on 2010-04-13T06:49:46Z Indexed on 2010/04/17 8:43 UTC
Read the original article Hit count: 152

Filed under:
|

Apologies if the question sounds silly, I was following experts in SO and trying some examples myself, and this is one of them. I did try the search option but didn't find an answer for this kind.

class A
{
    public:
         A(){cout<<"A Contruction"<<endl;}
        ~A(){cout<<"A destruction"<<endl;}
};

int main()
{
    vector<A> t;
    t.push_back(A()); // After this line, when the scope of the object is lost.
}

Why is the destructor of the class called twice ?

© Stack Overflow or respective owner

Related posts about c++

Related posts about destructor