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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:26:38+00:00 2026-06-09T03:26:38+00:00

I am trying to do a deep copy of class B , but A

  • 0

I am trying to do a deep copy of class B, but A doesn’t get set.

Why does b3->print return a garbage number instead of 1?

From what I understand, b2 and b3 are both pointing to the same A object. but I created a new object on the heap with B’s copy constructor. so why are they both still poiting to the same object?

I hope this makes sense.

#include <cstdlib>
#include <iostream>

using namespace std;

class A{
      int num;
public:
       A(int n):num(n){ cout<< "A "<< num << " constructor" <<endl;}  
       ~A(){ cout<< "A "<< num <<" destructor. " <<endl; }   

       int print(){
        cout<< num<< endl;
       }
};

class B{
      A *a;
      int num;
public:
       B(int n):num(n){
           a = new A(n);
           cout<< "B "<< num <<" constructor" <<endl;    
       }  
       ~B(){
            delete a; 
            cout<< "B "<< num <<" destructor"<<endl; 
       }    
       // Copy contructor
       B(const B & b): a(new A(b.num)){ 
       } 

       <strike>int<\strike> void print(){
        cout<< num << endl;
       }

       int get_num(){
           return num;
       }
};

int main(int argc, char *argv[])
{ 
    B *b2 = new B(1);
    B *b3(b2);
    b2->print();
    delete b2;
    b3->print();
    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 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-09T03:26:39+00:00Added an answer on June 9, 2026 at 3:26 am

    B *b3(b2); doesn’t do what you think.

    It’s equivalent to B* b3 = b2. The pointers will point to the same location. When you do delete b2;, you’re also freeing the memory pointed to by b3.

    To do a deep copy, do:

     B* b3 = new B(*b2);
    

    There’s also undefined behaviour here:

    int print(){
       cout<< num << endl;
    }
    

    because you never return. Change the return type to void.

    To get the value expected:

    B(const B & b): a(new A(b.num)), num(b.num){ 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this exercise on a python exam. Trying to return a deep o copy
I have some code that performs a deep copy using Object.clone, but I'm trying
I've been trying to do deep copy of a 2 dimensional array but never
I'm trying to make a deep copy of an object, including a GregorianCalendar instance.
Trying to copy the msdn refernce here doesn't work and gives an error I
I am trying to overload the assignment operator to do a deep copy of
I'm trying to just get rid of duplicate consecutive words from a text file,
I am trying to create a deep-copy of a NSMutableDictionary and assign it to
I have a compiler error when trying to set a generic base collection class
I am trying to create a deep copy of my binary tree data structure

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.