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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:49:16+00:00 2026-05-15T06:49:16+00:00

I have the following code #include <iostream> using namespace std; class Object { public:

  • 0

I have the following code

#include <iostream>
using namespace std;

class Object {

public:
   Object(int id){
     cout << "Construct(" << id << ")" << endl; 
     m_id = id;       
   }

   Object(const Object& obj){
      cout << "Copy-construct(" << obj.m_id << ")" << endl;  
      m_id = obj.m_id;
   }

   Object& operator=(const Object& obj){
      cout << m_id << " = " << obj.m_id << endl; 
      m_id = obj.m_id;
      return *this; 
   }

   ~Object(){
       cout << "Destruct(" << m_id << ")" << endl; 
   }
private:
   int m_id;

};

Object func(Object var) { return var; }

int main(){
   Object v1(1);
   cout << "( a )" << endl;
   Object v2(2);
   v2 = v1;
   cout << "( b )" << endl;
   Object v4 = v1;
   Object *pv5;
   pv5 = &v1;
   pv5 = new Object(5);
   cout << "( c )" << endl;
   func(v1);
   cout << "( d )" << endl;
   delete pv5;
}

which outputs

Construct(1)
( a )
Construct(2)
2 = 1
( b )
Copy-construct(1)
Construct(5)
( c )
Copy-construct(1)
Copy-construct(1)
Destruct(1)
Destruct(1)
( d )
Destruct(5)
Destruct(1)
Destruct(1)
Destruct(1)

I have some issues with this, firstly why does Object v4 = v1; call the copy constructor and produce Copy-construct(1) after the printing of ( b ).

Also after the printing of ( c ) the copy-constructor is again called twice?, Im not certain of how this function works to produce that
Object func(Object var) { return var; }

and just after that Destruct(1) gets called twice before ( d ) is printed.

sorry for the long question, I’m confused with the above.

  • 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-15T06:49:17+00:00Added an answer on May 15, 2026 at 6:49 am
    Object v1(1);
    // Construct(1)
    

    Regular constructor call for an automatic stack variable (destroyed at the end of the function).

    cout << "( a )" << endl;
    // ( a )
    
    Object v2(2);
    // Construct(2)
    

    Another constructor call.

    v2 = v1;
    // 2 = 1
    

    The assignment operator is called because v2 was already created (we called the constructor for it) and now we’re assigning one existing object to another.

    cout << "( b )" << endl;
    // ( b )
    
    Object v4 = v1;
    // Copy-construct(1)
    

    The copy constructor is called here because Object v4 is still not created, so we create it as a copy of v1. The assignment is taken here to mean the same as if you did Object v4(v1)

    Object *pv5;
    pv5 = &v1;
    pv5 = new Object(5);
    // Construct(5)
    

    Call the constructor for a heap object (destroyed explicitly with delete).

    cout << "( c )" << endl;
    // ( c )
    
    func(v1);
    // Copy-construct(1) <br />
    // Copy-construct(1) <br />
    // Destruct(1) <br />
    // Destruct(1) <br />
    

    The copy constructor is first called to copy v1 to the parameter var. It is called again to create a copy of var as return value to the caller. var is destroyed as it’s popped off the stack when exiting the function. The return value is destroyed after at the expression func(v1).

    cout << "( d )" << endl;
    // ( d )
    
    delete pv5;
    // Destruct(5)
    

    The object pointed at by pv5 is manually destroyed.

    } // end of main
    // Destruct(1) <br />
    // Destruct(1) <br />
    // Destruct(1) <br />
    

    The automatic variables v1, v2, v4 (all having copied the id of v1 from either assignment or copy construction) are popped off the stack and the destructor is called for each.

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

Sidebar

Ask A Question

Stats

  • Questions 435k
  • Answers 435k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can add a category into your template, then it… May 15, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer In cases where I need to communicate from my ViewModel… May 15, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer Just to close the loop on this one - it… May 15, 2026 at 3:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.