It's not even possible. When you "put an object into a container", you
are actually putting a *copy* of the object into the container, invoking
the copy constructor of the object's class.
To prevent the copying, you can store a *pointer* to the object into a
suitably-declared container (e.g. vector<foo*> instead of vector<foo>),
but in that case, erasing the pointer has no effect on the object itself.
Jon Bell is absolutely right. I am attaching some source code that I
wrote to test out the STL behavior with this posting. My apologies if
I am breaking the netiquette of this group.
Here are my findings....
· If the collection stores values rather than pointers (e.g.
list<Class_XYZ>), a copy of the entity is dynamically created, using
the copy constructor, and stored in the collection
· If the collection stores pointers rather than values (e.g. list<
Class_XYZ*>), the entity itself is stored
· If the collection stores values rather than pointers, upon invoking
erase(), the copy of the entity (that was stored in the collection)
gets deleted (using delete, since the destructor gets invoked). The
original entity is left untouched
· If the collection stores pointers rather than values, upon invoking
erase(), the entity is merely removed from the collection but does not
get deleted
In other words....
1) erase() merely removes an element from a collection, and does not
free up the associated memory
2) If the entity is an object, it is copied with the copy constructor
and deleted with the destructor.
3) If the entity is a pointer, the pointer is copied by value and the
storage for the pointer is subsequently released. Releasing the
pointer does not affect the pointed-to object.
Regards,
KP Bhat
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Element.h~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <time.h>
#include <iostream.h>
class Element
{
public:
Element(time_t i, char x[])
{
m_attr = i;
strcpy(m_label, x);
cout << "CTOR:: Element " << dec << m_attr << ", Pointer " << hex
<< this << ", " << m_label << endl;
}
Element(const Element& elem)
{
m_attr = elem.m_attr;
strcpy(m_label, elem.m_label);
cout << "COPY-CTOR:: Element " << dec << m_attr << ", Pointer " <<
hex
<< this << ", " << m_label << endl;
}
~Element()
{
cout << "DTOR:: Element " << dec << m_attr << ", Pointer " << hex
<< this << ", " << m_label << endl;
}
void display()
{
cout << ctime(&m_attr);
}
void printAddress() const
{
cout << hex << this << endl;
}
bool operator<(const Element& elem) const
{
bool retVal = (m_attr < elem.m_attr);
return retVal;
}
protected:
time_t m_attr;
char m_label[80];
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Main.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "Element.h"
#ifdef VECTOR
#include <vector>
#elif defined LIST
#include <list>
#elif defined DEQUE
#include <deque>
#elif defined SET
#include <set>
#elif defined MULTISET
#include <multiset.h>
#endif
void
main()
{
#ifdef VECTOR
cout << "Vector test\n\n";
#elif defined LIST
cout << "List test\n\n";
#elif defined DEQUE
cout << "Deque test\n\n";
#elif defined SET
cout << "Set test\n\n";
#elif defined MULTISET
cout << "Multiset test\n\n";
#endif
Element elem1(132, "VALUE");
Element elem2(232, "VALUE");
Element elem3(332, "VALUE");
Element *elem4 = new Element(432, "POINTER");
Element *elem5 = new Element(532, "POINTER");
Element *elem6 = new Element(632, "POINTER");
Element elem7(732, "VALUE --- will store address in Ptr
collection");
Element elem8(832, "VALUE --- will store address in Ptr
collection");
Element elem9(932, "VALUE --- will store address in Ptr
collection");
Element *elem10 = &elem7;
Element *elem11 = &elem8;
Element *elem12 = &elem9;
cout << endl;
#ifdef VECTOR
vector<Element> collElem;
vector<Element*> collElemPtr;
vector<Element>::iterator valueItor;
vector<Element*>::iterator pointerItor;
cout << "Inserting elements in collection-1\n";
collElem.push_back(elem1);
collElem.push_back(elem2);
collElem.push_back(elem3);
cout << "Inserted elements in collection-1\n\n";
cout << "Inserting elements in collection-2\n";
collElemPtr.push_back(elem4);
collElemPtr.push_back(elem5);
collElemPtr.push_back(elem6);
collElemPtr.push_back(elem10);
collElemPtr.push_back(elem11);
collElemPtr.push_back(elem12);
cout << "Inserted elements in collection-2\n\n";
#elif defined LIST
list<Element> collElem;
list<Element*> collElemPtr;
list<Element>::iterator valueItor;
list<Element*>::iterator pointerItor;
cout << "Inserting elements in collection-1\n";
collElem.push_back(elem1);
collElem.push_back(elem2);
collElem.push_back(elem3);
cout << "Inserted elements in collection-1\n\n";
cout << "Inserting elements in collection-2\n";
collElemPtr.push_back(elem4);
collElemPtr.push_back(elem5);
collElemPtr.push_back(elem6);
collElemPtr.push_back(elem10);
collElemPtr.push_back(elem11);
collElemPtr.push_back(elem12);
cout << "Inserted elements in collection-2\n\n";
#elif defined DEQUE
deque<Element> collElem;
deque<Element*> collElemPtr;
deque<Element>::iterator valueItor;
deque<Element*>::iterator pointerItor;
cout << "Inserting elements in collection-1\n";
collElem.push_back(elem1);
collElem.push_back(elem2);
collElem.push_back(elem3);
cout << "Inserted elements in collection-1\n\n";
cout << "Inserting elements in collection-2\n";
collElemPtr.push_back(elem4);
collElemPtr.push_back(elem5);
collElemPtr.push_back(elem6);
collElemPtr.push_back(elem10);
collElemPtr.push_back(elem11);
collElemPtr.push_back(elem12);
cout << "Inserted elements in collection-2\n\n";
#elif defined SET
set<Element> collElem;
set<Element*> collElemPtr;
set<Element>::iterator valueItor;
set<Element*>::iterator pointerItor;
cout << "Inserting elements in collection-1\n";
collElem.insert(elem1);
collElem.insert(elem2);
collElem.insert(elem3);
cout << "Inserted elements in collection-1\n\n";
cout << "Inserting elements in collection-2\n";
collElemPtr.insert(elem4);
collElemPtr.insert(elem5);
collElemPtr.insert(elem6);
collElemPtr.insert(elem10);
collElemPtr.insert(elem11);
collElemPtr.insert(elem12);
cout << "Inserted elements in collection-2\n\n";
#elif defined MULTISET
multiset<Element> collElem;
multiset<Element*> collElemPtr;
multiset<Element>::iterator valueItor;
multiset<Element*>::iterator pointerItor;
cout << "Inserting elements in collection-1\n";
collElem.insert(elem1);
collElem.insert(elem2);
collElem.insert(elem3);
cout << "Inserted elements in collection-1\n\n";
cout << "Inserting elements in collection-2\n";
collElemPtr.insert(elem4);
collElemPtr.insert(elem5);
collElemPtr.insert(elem6);
collElemPtr.insert(elem10);
collElemPtr.insert(elem11);
collElemPtr.insert(elem12);
cout << "Inserted elements in collection-2\n\n";
#endif
cout << "\n\nTraversing collElem collection (collection-1)" << endl;
for(valueItor = collElem.begin(); valueItor != collElem.end();
valueItor++)
{
cout << "The object pointer is ";
valueItor->printAddress();
}
cout << "Traversed collElem collection (collection-1)" << endl;
cout << "\n\nTraversing collElemPtr collection (collection-2)" <<
endl;
for(pointerItor = collElemPtr.begin(); pointerItor !=
collElemPtr.end();
pointerItor++)
{
Element* elemPtr = *pointerItor;
cout << "The object pointer is ";
elemPtr->printAddress();
}
cout << "Traversed collElemPtr collection (collection-2)" << endl;
cout << "\n\nErasing collElem collection (collection-1)" << endl;
collElem.erase(collElem.begin(), collElem.end());
cout << "Erased collElem (collection-1) entirely\n\n" << endl;
cout << "\n\nErasing collElemPtr collection (collection-2)" << endl;
collElemPtr.erase(collElemPtr.begin(), collElemPtr.end());
cout << "Erased collElemPtr (collection-2) entirely\n\n" << endl;
return 0;
}