V
velthuijsen
just found the STL and now I'm mucking around with it to see how it
works.
First thing that I've found out is that I appearantly have no clue how
copying works inside the STL (or more likely no clue how it works in
general).
So I've got two questions
1) Where can I find a good explanation on how this works on the
internet?
2) Is the stumbling around I've done so far not to horrifying?
What I did was make my own class (including a constructor/destructor
to see how they would get called).
Then I added the following to the main.
list<MyOwnClass> MyOwnClassTestList; // global
void AddItemToTestList()
{
MyOwnClass TestItem;
MyOwnClassTestList.push_back(TestItem);
}
This resulted in the constructor being called once (at MyOwnClass
TestItem
and the destructor called twice (the } of AddItemToTestList is one
place, the other the place where I do MyOwnClassTestList.pop_front());
)
My current solutions to the double call to the destructor are the
following:
void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(*TestItem);
TestItem = NULL;
}
or
list<MyOwnClass*> MyOwnClassTestList; // global
void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(TestItem);
TestItem = NULL;
}
void RemoveItemFromTestList()
{
MyOwnClass *TestItem = MyOwnClassTestList.front();
MyOwnClassTestList.pop_front;
delete TestItem;
}
Is this the correct way to handle this situation? And if not what
should I do differently?
works.
First thing that I've found out is that I appearantly have no clue how
copying works inside the STL (or more likely no clue how it works in
general).
So I've got two questions
1) Where can I find a good explanation on how this works on the
internet?
2) Is the stumbling around I've done so far not to horrifying?
What I did was make my own class (including a constructor/destructor
to see how they would get called).
Then I added the following to the main.
list<MyOwnClass> MyOwnClassTestList; // global
void AddItemToTestList()
{
MyOwnClass TestItem;
MyOwnClassTestList.push_back(TestItem);
}
This resulted in the constructor being called once (at MyOwnClass
TestItem
and the destructor called twice (the } of AddItemToTestList is one
place, the other the place where I do MyOwnClassTestList.pop_front());
)
My current solutions to the double call to the destructor are the
following:
void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(*TestItem);
TestItem = NULL;
}
or
list<MyOwnClass*> MyOwnClassTestList; // global
void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(TestItem);
TestItem = NULL;
}
void RemoveItemFromTestList()
{
MyOwnClass *TestItem = MyOwnClassTestList.front();
MyOwnClassTestList.pop_front;
delete TestItem;
}
Is this the correct way to handle this situation? And if not what
should I do differently?