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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:30:16+00:00 2026-06-04T22:30:16+00:00

While doing my programming assignments, I seem to be stumbling over basic C++ concepts.

  • 0

While doing my programming assignments, I seem to be stumbling over basic C++ concepts. I found the bug in my program and it was caused by my destructor running more times than I expected. Here is a code sample demonstrating what I am doing wrong, down to the bare essentials.

#include <iostream>
using namespace std;

class A
{
public:
    A(int num)
    {
        number = num;
        cout << "A constructed with number " << number << ".\n";
    }
    ~A()
    {
        cout << "A destructed with number " << number << ".\n";
    }
private:
    int number;
};

class B
{
public:
    B(A pa)
        : a(pa)
    {
        cout << "B constructor run.\n";
    }
    ~B()
    {
        cout << "B destructor run.\n";
    }
private:
    A a;
};


int main()
{
    A foo(7);
    {
        B bar(foo);
    }
    //Pause the program.
    system("pause");
}

What I expect to happen is A foo(7); allocates space on the stack for an A object named foo and call the constructor, passing 7. It assigns 7 to number and prints output indicating the the constructor ran. Now B bar(foo); allocates space on the stack for a B object named bar and calls the constructor, passing foo by value, which is just a container for an int. The constructor assigns the A parameter passed to it to it’s own private data member a, and prints output to the screen.

Now, when bar goes out of scope at the closing curly brace, I expect bar‘s destructor to be called, which prints output to the screen, then calls the destructor for its data members, namely A a. That destructor prints output to the screen, and discards the int number that it was containing.

What I expect the output should be:

A constructed with number 7.
B constructor run.
B destructor run.
A destructed with number 7.
//Destructors should be called in the reverse order of their construction right?

The actual output:

A constructed with number 7.
B constructor run.
A destructed with number 7. //This is unexpected.
B destructor run.
A destructed with number 7.

What is causing that extra destruction?

  • 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-04T22:30:18+00:00Added an answer on June 4, 2026 at 10:30 pm

    Obviously it comes from member data A a; of class B. I guess your doubt is that why not see any construct output from A, because it’s constructed with default copy-ctor for class A, It’s better to add one copy-ctor for class A, so that you will see the construct procedure.

    A(const A& a)
    {
         number = a.number;
         cout << "A copy-constructed with number " << number << ".\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While doing some random experimentation with a factorial program in C, Python and Scheme.
While doing some refactoring I've found that I'm quite often using a pair or
I've been doing socket programming for a while in C++, and kind of got
After doing the whole enterprise programming for a while, I'm seriously disillusioned by the
I am new to opengl programming, but I am doing something very basic, and
This is something I would like to see while doing my day today programming
While doing concurrent programming I need to tell the compiler/optimizer that it may not
I've been doing some simple web programming using python, and I have a basic
I wanted to know how to flush the socket streams while doing socket programming
While doing variadic template programming in C++11 on GCC, once in a while I

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.