|
-
October 2nd, 2009, 06:10 AM
#1
Need help with vector of pointers
Hi,
I'm very new to programming and have up until now created procedural models without any dynamic memory etc. Ultimately I will be creating models of individuals that will move around on a grid, breed and then die and so first have tried to play around with some very simple code to get me started but am having difficulty getting my head around it. I was aiming to store instances of a class in a vector of pointers and so have been playing around with the following code which has 2 of these vectors. In the code the class only holds 3 data members as an example, adds instances and assigns them etc. Not using iterators as yet but aim to do so.
The problem I'm having is that when I (try to!) delete an instance of the class pointed to by the vectors it only seems to delete the m_Species and m_Colour data members as it will still output m_Name and therefore create a memory leak when I clear the vector. Is this something to do with not having a copy constructor and destructor? I tried creating a destructor but couldn't get that to work. Do the data members also need to be pointers as well for any of this to work? Part of the problem is I'm not familiar with using a vector of pointers to objects with data members!
Any help is very much appreciated!
Thanks
#pragma hdrstop
#include <vector>
#include <deque>
#include <string>
#include <iostream>
using namespace std;
//---------------------------------------------------------------------------
#pragma argsused
class Animal{
public:
static int s_total;
Animal(string word="zero", string word2="monkey",int number=0);
//~Animal();
//Animal(const Animal& c); //copy constructor prototype
static int GetTotal();
string m_Species;
string m_Colour;
int m_Name;
};
vector<Animal*> Adults;
vector<Animal*> Juvs;
int Animal::s_total=0;
Animal::Animal(string word, string word2, int number):m_Colour(word),m_Species(word2),m_Name(number) //constructor
{++s_total;}
int Animal::GetTotal(){
return s_total;
}
/*Animal::~Animal(){ //destructor definition
cout<<"Destructor called\n";
//delete m_Name;
//delete m_Colour;
} */
void printAdults();
void printJuvs();
int main(int argc, char* argv[])
{
Adults.push_back(new Animal("red","bird",1));
Adults.push_back(new Animal("blue", "cat",2));
Adults.push_back(new Animal("green", "dog",3));
Adults.push_back(new Animal("yellow", "rabbit",4));
//delete Adults[2];
printAdults();
Juvs.assign(Adults.begin(),Adults.end());
printAdults();
//printJuvs();
Juvs.push_back(new Animal("red", "bird",11));
Juvs.push_back(new Animal("blue", "cat",12));
Juvs.push_back(new Animal("green","dog",13));
Juvs.push_back(new Animal("yellow","rabbit",14));
printJuvs();
Adults.clear();
for(int i=0;i<Juvs.size();i++){
if(Juvs[i]->m_Name==2||Juvs[i]->m_Name==12){
Adults.push_back(Juvs[i]);
}
else{delete Juvs[i];
--Animal::s_total;}
}
//Juvs.clear();
printAdults();
printJuvs();
cout<<"Adults size is: "<<Adults.size()<<endl;
// cout<<"Adults capacity is: "<<Adults.capacity()<<endl;
cout<<"Juvs size is: "<<Juvs.size()<<endl;
//cout<<"Juvs capacity is: "<<Juvs.capacity()<<endl;
cout<<Animal::GetTotal()<<"\n"<<endl;
//for(int i=0;i<Juvs.size();i++){
if(Adults[0]->m_Name==2){
delete Adults[0];
}
// }
printAdults();
cin.get();
return 0;
}
void printAdults (){
for(int i=0;i<Adults.size();i++){
cout<< Adults[i]<<" "<<Adults[i]->m_Name<<" "<<Adults[i]->m_Colour<<" "<<Adults[i]->m_Species<<endl;
}
cout<<"\n"<<endl;
}
void printJuvs(){
for(int i=0;i<Juvs.size();i++){
cout<<Juvs[i]<<" "<<Juvs[i]->m_Name<<" "<<Juvs[i]->m_Colour<<" "<<Juvs[i]->m_Species<<endl;
}
cout<<"\n"<<endl;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|