Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7665231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:30:38+00:00 2026-05-31T14:30:38+00:00

I was trying to practice on the following code: #include <iostream> #include <vector> using

  • 0

I was trying to practice on the following code:

#include <iostream>
#include <vector>

using namespace std;

class A
{
public:
   virtual void f(){cout<<"A"<<endl;}
   virtual ~A(){cout<<"destruct A"<<endl;}
};

int main()
{
   A o1,o2;
   vector <A > O;
   O.push_back(o1);
   cout<<"1"<<endl;
   O.push_back(o2);

cout<<"test"<<endl;
return 0;
}

The results turned out to be:

1
destruct A
test
destruct A
destruct A
destruct A
destruct A

Get confused about where the first destructor comes from.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:30:40+00:00Added an answer on May 31, 2026 at 2:30 pm

    After we had print statements to the copy constructor we get this:

    constructor A(0x7fff6e21e800)
    constructor A(0x7fff6e21e7f8)
    copy A(0x10e700910: From 0x7fff6e21e800)
    1
    copy A(0x10e700920: From 0x10e700910)
    copy A(0x10e700928: From 0x7fff6e21e7f8)
    destruct A(0x10e700910)
    test
    destruct A(0x10e700920)
    destruct A(0x10e700928)
    destruct A(0x7fff6e21e7f8)
    destruct A(0x7fff6e21e800)
    

    So now lets look at the code:

    int main()
    {
       A o1,
         // constructor A(0x7fff6e21e800)
             o2;
         // constructor A(0x7fff6e21e7f8)
       vector <A > O;
       O.push_back(o1);
         // copy A(0x10e700910: From 0x7fff6e21e800)
       cout<<"1"<<endl;
         // 1
    
       O.push_back(o2);
        // copy A(0x10e700920: From 0x10e700910)    // O needs to expand.
                                                    // So a new range is created and the old value
                                                    // copied from the old range to the new range.
    
                                                    // Now we push o2 into the vector
        // copy A(0x10e700928: From 0x7fff6e21e7f8)
    
                                                    // Now the old range has to be destroyed.
        // destruct A(0x10e700910)
    
    
       cout<<"test"<<endl;
        // test
    
    
       return 0;
                     // Before we exit destroy the old vector (of two items)
        // destruct A(0x10e700920)
        // destruct A(0x10e700928)
    
                    // Now destroy o2 then o1
        // destruct A(0x7fff6e21e7f8)
        // destruct A(0x7fff6e21e800)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand what const_iterator means. I have the following example code: void
I am trying to set and read cookies using the following code cookie_name =
I'm trying to practice writing better code, so I wanted to validate my input
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects to
Im trying to find a best practice to load usercontrols using Ajax. My first
I'm trying to design a class structure with the following: Container class which holds
I'm trying to figure out what the best practice is for using jQuery in
Recently I was trying some practice programs in python and I came across this
I'm trying to practice my F# by writing small console scripts in F# in
I am fairly new to vim. I am trying to practice (been reading a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.