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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:30:17+00:00 2026-06-10T04:30:17+00:00

I am a very new programmer and a super beginner so I don’t know

  • 0

I am a very new programmer and a super beginner so I don’t know too much about c++. I had specifically a question regarding making deep copies of pointers. What I have is a class A full of POD’s and a pointer to this class (A *P).
I have a second class B which contains some other POD’s and a vector of pointers to Class A.
I want to fill this vector of deep copies of A *P because in a loop I will be dynamically allocating and deallocating it. The following does not work. I believe its my copy constructor and overloading of the = operator. This is something I am doing for fun and learning.

class A
{
   public:  
   .....
   .....
   .....
};

class B
{
   public:  
   B();
  ~B();
   B(const B &Copier);
   B& B::operator=(const B &Overloading);
   vector<A*> My_Container;
   A* Points_a_lot;
   int counter;
 };
B::B()
{
  counter=0;
  Points_a_lot=NULL;
}
B::~B()
{
   for(size_t i=0; i<My_Container.size(); i++)
   {
      delete My_Container[i];
    }
 }
 B::B(const B &Overloading)
 {
     My_Container[counter]=new A(*Overloading.Points_a_lot);
 }
 B& B::operator=(const B &Overloading)
 {
     if(!Overloading.My_Container.empty()) 
     {
         Overloading.My_Container[counter]=new B(*Overloading.Points_a_lot);
      }
      return *this; 
  } 
 int main()
 {  A* p=NULL;
    B Alphabet;
    for(....)
    {
        p=new A;
        //some stuff example p->Member_of_A=3; etc..
        Alphabet.My_Container[Alphabet.counter]=p;
        Alphabet.counter++;
       delete p;
     }
    return 0;
   }

Any help will be great. I thank you for your time. Assume needed libraries included.

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

    I do not fully understand what your requirements are so I attempted to fix the code and do a deep copy of B as that is what it seems you are asking.

    #include <vector>
    using namespace std;
    
    class A
    {
    public:
        A() : m_someInt(0), m_someFloat(0.0f) {}
    
        // Redundant but putting it here for you to see when it is called (put break-point)
        A(const A& a_other)
        {
            m_someInt = a_other.m_someInt;
            m_someFloat = a_other.m_someFloat;
        }
    
        int   m_someInt;
        float m_someFloat;
    };
    
    class B
    {
    public:  
        B();
        ~B();
        B(const B &Copier);
        B& B::operator=(const B &Overloading);
        void Cleanup();
        void AddA(const A* a);
    
    private:
    
        vector<A*> My_Container;
        A* Points_a_lot;
    };
    
    B::B()
    {
        Points_a_lot=NULL;
    }
    
    B::~B()
    {
        Cleanup();
    }
    
    B::B(const B &Overloading)
    {
        // Deep copy B
        operator=(Overloading);
    }
    
    B& B::operator=(const B &Overloading)
    {
        // Delete old A's
        Cleanup();
    
        // Not using iterators to keep it simple for a beginner
        for (size_t i = 0; i < Overloading.My_Container.size(); ++i)
        {
            // We need new A's which will then copy from the A's in Overloading's container
            A* newA = new A( *(Overloading.My_Container[i]) );
            // Done with allocation and copy, push_back to our container
            My_Container.push_back(newA);
        }
    
        return *this; 
    }
    
    void B::Cleanup()
    {
        // Assuming B is not responsible for cleaning up Points_a_lot
        Points_a_lot = NULL;
    
        for (size_t i = 0; i < My_Container.size(); ++i)
        {
            delete My_Container[i];
        }
    
        // Automatically called when My_Container is destroyed, but here we 
        // are open to the possibiliy of Cleanup() being called by the client
        My_Container.clear(); 
    }
    
    void B::AddA(const A* a)
    {
        // We are adding a new A. In your code, the incoming A is going to 
        // be destroyed, therefore, we need to allocate a new A and copy 
        // the incoming A
        A* newA = new A(*a);
        My_Container.push_back(newA);
    }
    
    int main()
    {  
        A* p=NULL;
        B Alphabet;
        for(int i = 0; i < 10; ++i)
        {
            p = new A();
            //some stuff example p->Member_of_A=3; etc..
            Alphabet.AddA(p);
            delete p;
        }
    
        // If you put a breakpoint here and step through your code, you will see
        // `B` deep-copied
        B NewAlphabet = Alphabet;
    
        return 0;
    }
    

    A few notes:

    • Newing and deleteing A in the loop is a bad idea. I realize you are doing this just to learn, which is great, but you may want to keep this in mind. Instead of destroying A through p, allow B to take ownership of the new A
    • Step through the code by using a debugger to see how it works
    • Try to post code that is as close to your original as possible when asking “why doesn’t this work/compile”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like the title. As a new programmer,It is very important to know the mechanism
I am a very new programmer, I have made a couple basic applications, however
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
I'm a fairly new programmer and very new to web programming and I need
HI I am a new java programmer (very new). What I want to do/test
I'm Delphi programmer and very new to Cocoa. at first I tried this :
I am an ASP.NET WebForms programmer and I'm very new to ASP.NET MVC3. I've
I am a rather new C++ programmer. I have made a very simple game
I'm a Java/J2ee programmer working in India. I'm very passionate about programming and I
I am a very new programmer.. A website is providing a lot of zip

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.