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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:12:50+00:00 2026-06-09T23:12:50+00:00

Today I was wondering about c++ destructors so I wrote a small test program.

  • 0

Today I was wondering about c++ destructors so I wrote a small test program. That answered my original question but raised a new one which is:
The following program:

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
class test
{
public:
    int id;
    vector<test> collection;
    test(){}
    test(int id_in){id = id_in;}
    ~test(){cout << "dying: " << id << "\n";}
};

int _tmain(int argc, _TCHAR* argv[])
{
    {
        test obj(1);
        obj.collection.push_back(test(2));
        obj.collection.push_back(test(3));
        cout << "before overwrite\n";
        obj = test(4);
        cout << "before scope exit\n";
    }
    int x;
    cin >> x;
}

produces the following output:

dying: 2
dying: 2
dying: 3
before overwrite
dying: 2
dying: 3
dying: 4
before scope exit
dying: 4

Why don’t I see a destructor for my test object with id 1? If its destructor isn’t called when it is overwritten, than what calls the destructors of the instances in its vector?

  • 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-06-09T23:12:52+00:00Added an answer on June 9, 2026 at 11:12 pm

    You violate the Rule of Three by creating a destructor, but no assignment operator.

    From reading that, you can interpret your code as follows:

    When the line

    obj = test(4);
    

    is compiled, a temporary instance of test is created with id 4.

    Then, the assignment operator is called. Since you did not provide one, the compiler generated one for you that looks like this:

    test& operator=(const test& other)
    {
        id = other.id;
        collection = other.collection;
        return *this;
    }
    

    The id 1 is simply overwritten with the 4 from the temporary, and for the collection assignment, the assignment operator of std::vector is called.

    std::vector‘s assignment operator deletes all previously contained elements, which is why you see

    dying: 2
    dying: 3
    

    in your output. Finally, the temporarily created obj instance with id 4 is deleted, causing

    dying: 4
    

    to appear for the first time. When obj goes out of scope, you see the

    dying: 4
    

    output once more.

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

Sidebar

Related Questions

This is a pretty general question, but I was wondering today about delegates. At
Today I came up with a question about the web application conventions. For the
another day, another problem :) Since I woke up today I'm wondering about numbers.
I was wondering today about how much code people normally have in a single
I read something about Lua today, and I was wondering what it was. I
This may be a very noobie question, but in today's world of web app
I have come across a situation today which has me wondering about best practices.
I was asked earlier today about adding WordPress to a website and was wondering
Earlier today I asked a question about passing dictionary values to a function. While
I encountered some curious behavior today and was wondering if it is expected or

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.