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 1030977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:51:23+00:00 2026-05-16T13:51:23+00:00

Here is the code (also at http://pastebin.com/yw5z2hnG ): #include <iostream> #include <vector> using namespace

  • 0

Here is the code (also at http://pastebin.com/yw5z2hnG ):

#include <iostream>
#include <vector>
using namespace std;

class X
{
    public:
    int i;
    X();
    ~X();
};

X::X()
{
    i = 1;
    cout << "---constructor" << '\n';
}

X::~X()
{
    cout << "***desctructor" << '\n';
}

int main()
{
    vector<X> *vx = new vector<X>;
    cout << "------------------------------------" << endl;
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    cout << "------------------------------------" << endl;
    delete vx;
}

I get the output as:

------------------------------------
---constructor
***desctructor
---constructor
***desctructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
---constructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
------------------------------------
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor

I do not understand why so many destructors are called.

  • 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-16T13:51:23+00:00Added an answer on May 16, 2026 at 1:51 pm

    If you define your own copy constructor you will see the other objects being constructed:

    class X
    {
        public:
        int i;
        X(const X&);
        X();
        ~X();
    };
    
    X::X(const X& x) : i( x.i )
    {
        cout << "---copy constructor\n";
    }
    
    // ... rest as before
    

    The compiler will provide a copy constructor that performs no logging if you don’t declare your own one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
I'm making a c++ rpg. here is my code: http://pastebin.com/cyR5bzKR And it let's me
full code here... http://pastebin.com/EEnm8vi3 line 378 is not inserting the sections into the current
There’s a really cool diff class hosted by Google here: http://code.google.com/p/google-diff-match-patch/ I’ve used it
I want to run a simple iText program, here is my code: http://pastebin.com/C6YMkYNk It
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Here is code example class A{ int i; public: A(int i) : i(i) {}
I have a table called informations (http://pastebin.com/h3UiUhpx) and also have this associated tables educations,
I have uploaded my code to pastebin, this is the link: http://pastebin.com/wBu9PP2x When i
It is said here http://msdn.microsoft.com/en-us/library/fa13yay7%28v=VS.90%29.aspx (see also http://msdn.microsoft.com/en-us/library/s4wcexbc%28VS.90%29.aspx ) Use this option if you

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.