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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:51:51+00:00 2026-05-24T23:51:51+00:00

Here is the code which confuses me: #include <iostream> using namespace std; class B

  • 0

Here is the code which confuses me:

#include <iostream>
using namespace std;

class B {
public:
    B() {
        cout << "constructor\n";
    }
    B(const B& rhs) {
        cout << "copy ctor\n";
    }
    B & operator=(const B & rhs) {
        cout << "assignment\n";
    }
    ~B() {
        cout << "destructed\n";
    }
    B(int i) : data(i) {
        cout << "constructed by parameter " << data << endl;
    }

private:
    int data;
};

B play(B b)
{
    return b;
}

int main(int argc, char *argv[])
{
#if 1
    B t1;
    t1 =  play(5);
#endif

#if 0
    B t1 = play(5);
#endif

    return 0;
}

Environment is g++ 4.6.0 on Fedora 15.
The first code fragment output is as follows:

constructor
constructed by parameter 5
copy ctor
assignment
destructed
destructed
destructed

And the second fragment code output is:

constructed by parameter 5
copy ctor
destructed
destructed

Why are are three destructors are called in the first example, while in the second it is only two?

  • 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-24T23:51:51+00:00Added an answer on May 24, 2026 at 11:51 pm

    First Case:

    B t1;
    t1 =  play(5);
    
    1. Creates a object t1 by calling default constructor of B.
    2. In order to call play(), A temporary object of B is created by using B(int i). 5 is passed as an and object of B is created, and play() is called.
    3. return b; inside play() causes the copy constructor to be called for returning a copy of object.
    4. t1 = calls the Assignemnt operator to assign the returned object copy to t1.
    5. First destructor, destructs the temporary object created in #3.
    6. Second destructor destructs the returned temporay object in #2.
    7. Third destructor destructs the object t1.

    Second case:

    B t1 = play(5);  
    
    1. An temporary object of class B is created by calling parameterized constructor of B which takes int as a paraemter.
    2. This temporary object is used to call the Copy constructor of class B.
    3. First destructor destructs the temporary created in #1.
    4. Second destructor destructs object t1.

    One destructor call is less in Second Case because, in second Case the compiler uses Return value Optimization and elides the call to create an additional temporary object while returning from play(). Instead the Base object is created in the location where the temporary would have been assigned.

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

Sidebar

Related Questions

#include <iostream> using namespace std; int main(int argc, char* argv[]) { int i1 =
Here is my code which checks if the file exists : #include<stdio.h> #include<zlib.h> #include<unistd.h>
Here is jquery code which hides my table cells with the ID of .style2:
Here is my code which is doing the conversion from hex to decimal. The
I have some code here which works perfectly in firefox but not in chrome
I'm just trying out the allegro library, and here is the code which I've
I am trying to understand this inline assembly code which comes from _hypercall0 here
I have this code here, which is intended to allow any type of arguments:
Here is my code, which takes two version identifiers in the form 1, 5,
Here is my code, which creates 2d array filled with zeros, array dimensions are

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.