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

  • SEARCH
  • Home
  • 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 730873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:57:42+00:00 2026-05-14T06:57:42+00:00

[a follow up to this question ] class A { public: A() {cout<<A Construction

  • 0

[a follow up to this question]

class A
{
    public:
         A()          {cout<<"A Construction"     <<endl;}
         A(A const& a){cout<<"A Copy Construction"<<endl;}
        ~A()          {cout<<"A Destruction"      <<endl;}
};

int main() {
    {
        vector<A> t;
        t.push_back(A());
        t.push_back(A());   // once more
    }
}

The output is:

A Construction        // 1
A Copy Construction   // 1
A Destruction         // 1
A Construction        // 2
A Copy Construction   // 2
A Copy Construction   // WHY THIS?
A Destruction         // 2
A Destruction         // deleting element from t
A Destruction         // deleting element from t
A Destruction         // WHY THIS?
  • 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-14T06:57:42+00:00Added an answer on May 14, 2026 at 6:57 am

    To clearly see what’s going on, I recommend include the this pointer in the output to identify which A is calling the method.

         A()          {cout<<"A (" << this << ") Construction"     <<endl;}
         A(A const& a){cout<<"A (" << &a << "->" << this << ") Copy Construction"<<endl;}
        ~A()          {cout<<"A (" << this << ") Destruction"      <<endl;}
    

    The output I’ve got is

    A (0xbffff8cf) Construction
    A (0xbffff8cf->0x100160) Copy Construction
    A (0xbffff8cf) Destruction
    A (0xbffff8ce) Construction
    A (0x100160->0x100170) Copy Construction
    A (0xbffff8ce->0x100171) Copy Construction
    A (0x100160) Destruction
    A (0xbffff8ce) Destruction
    A (0x100170) Destruction
    A (0x100171) Destruction
    

    So the flow can be interpreted as:

    1. The temporary A (…cf) is created.
    2. The temporary A (…cf) is copied into the vector (…60).
    3. The temporary A (…cf) is destroyed.
    4. Another temporary A (…ce) is created.
    5. The vector is expanded, and the old A (…60) in that vector is copied to the new place (…70)
    6. The other temporary A (…ce) is copied into the vector (…71).
    7. All unnecessary copies of A (…60, …ce) are now destroyed.
    8. The vector is destroyed, so the A’s (…70, …71) inside are also destroyed.

    Step 5 will be gone if you do

        vector<A> t;
        t.reserve(2); // <-- reserve space for 2 items.
        t.push_back(A());
        t.push_back(A());
    

    The output will become:

    A (0xbffff8cf) Construction
    A (0xbffff8cf->0x100160) Copy Construction
    A (0xbffff8cf) Destruction
    A (0xbffff8ce) Construction
    A (0xbffff8ce->0x100161) Copy Construction
    A (0xbffff8ce) Destruction
    A (0x100160) Destruction
    A (0x100161) Destruction
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This question is a follow-up to C# accessing protected member in derived class )
I have a class named Node as follow Public class T{ int value; public
Follow up to this question . I have the following code: string[] names =
Follow up to this question about GNU make : I've got a directory, flac
This question is a follow on from this one ... I am binding to
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends
This question is kind of a follow up to this question I asked a
This is a follow up of this question: Puzzle - How to 'colorify' part
As a follow up to this question , I have a parent entity that
This is a follow-up to this question. I am trying to build a mod_rewrite

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.