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.