T
Tony Johansson
Hello!
I have some problem with STL function remove
I have two classes called Handle which is a template class and Integer which
is not a template class. The Integer class is just a wrapper class for a
primitive int with some methods. I don't show the Integer class because it
will not add any information to my problem. Main is using some STL function
Now to my problem.
If I do this sequence in main first create two Handle object myh1 and myh2.
Then I use push_front to first push in myh1 and then myh2 into the list
Then I use the STL function for_each in this way
for_each(myList1.begin(), myList1.end(), handle_t() );
The function operator would write 2 and then 1 because I pushed_front the
Handle myh1 and then myh2
This function for_each will write out the entire list by calling the
function operator defined as
void operator()(Handle& temp) //Funktionsanropsoperator
{ cout << *temp.body << endl; }
for each Handle in the list. The function operator will write the primitive
ints because the wrapper class Integer has a type conversion operator that
convert from the class Integer to an int.
When I want to remove a Handle from the list I use the STL function remove
in this way
remove(myList1.begin(), myList1.end(), myh2);
Here I remove the Handle named myh2.
But now to the very strange thing. I would expect that the destructor for
Handle would be called when I do the remove but it doesn't. If I want to
write out the list after I have done the remove I use the for_each again in
this way.
for_each(myList1.begin(), myList1.end(), handle_t() );
The function operator would now write out 1 and then 1. It should only have
written 1 because the 2 should have been removed. The other strange thing is
that the removed 3 has been changed to 1 in some strange way that I don't
understand.
Have you any suggestion ?
//Tony
#include "handle.h"
#include <list>
#include <algorithm>
using namespace std;
typedef Handle<Integer> handle_t;
main()
{
list<handle_t> myList1;
handle_t myh1( new Integer(1) );
handle_t myh2( new Integer(2) );
myList1.push_front(myh1);
myList1.push_front(myh2);
for_each(myList1.begin(), myList1.end(), handle_t());
remove(myList1.begin(), myList1.end(), myh2);
for_each(myList1.begin(), myList1.end(), handle_t());
}
#include "integer.h"
#include <iostream>
using namespace std;
template<class T>
class Handle
{
public:
Handle()
{
body = new T(0);
ref_count = new int(1);
}
Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}
~Handle() //Destructor
{
(*ref_count)--;
if (!*ref_count)
deleteAll();
}
bool operator==(const Handle& temp)
{ return *body == *temp.body; }
Handle(const Handle& h) //Copy constructor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}
void operator()(Handle& temp) //Functionoperator
{ cout << * temp.body << endl; }
private:
T* body;
int* ref_count;
void deleteAll()
{
delete body;
body = NULL;
delete ref_count;
ref_count = NULL;
}
};
I have some problem with STL function remove
I have two classes called Handle which is a template class and Integer which
is not a template class. The Integer class is just a wrapper class for a
primitive int with some methods. I don't show the Integer class because it
will not add any information to my problem. Main is using some STL function
Now to my problem.
If I do this sequence in main first create two Handle object myh1 and myh2.
Then I use push_front to first push in myh1 and then myh2 into the list
Then I use the STL function for_each in this way
for_each(myList1.begin(), myList1.end(), handle_t() );
The function operator would write 2 and then 1 because I pushed_front the
Handle myh1 and then myh2
This function for_each will write out the entire list by calling the
function operator defined as
void operator()(Handle& temp) //Funktionsanropsoperator
{ cout << *temp.body << endl; }
for each Handle in the list. The function operator will write the primitive
ints because the wrapper class Integer has a type conversion operator that
convert from the class Integer to an int.
When I want to remove a Handle from the list I use the STL function remove
in this way
remove(myList1.begin(), myList1.end(), myh2);
Here I remove the Handle named myh2.
But now to the very strange thing. I would expect that the destructor for
Handle would be called when I do the remove but it doesn't. If I want to
write out the list after I have done the remove I use the for_each again in
this way.
for_each(myList1.begin(), myList1.end(), handle_t() );
The function operator would now write out 1 and then 1. It should only have
written 1 because the 2 should have been removed. The other strange thing is
that the removed 3 has been changed to 1 in some strange way that I don't
understand.
Have you any suggestion ?
//Tony
#include "handle.h"
#include <list>
#include <algorithm>
using namespace std;
typedef Handle<Integer> handle_t;
main()
{
list<handle_t> myList1;
handle_t myh1( new Integer(1) );
handle_t myh2( new Integer(2) );
myList1.push_front(myh1);
myList1.push_front(myh2);
for_each(myList1.begin(), myList1.end(), handle_t());
remove(myList1.begin(), myList1.end(), myh2);
for_each(myList1.begin(), myList1.end(), handle_t());
}
#include "integer.h"
#include <iostream>
using namespace std;
template<class T>
class Handle
{
public:
Handle()
{
body = new T(0);
ref_count = new int(1);
}
Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}
~Handle() //Destructor
{
(*ref_count)--;
if (!*ref_count)
deleteAll();
}
bool operator==(const Handle& temp)
{ return *body == *temp.body; }
Handle(const Handle& h) //Copy constructor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}
void operator()(Handle& temp) //Functionoperator
{ cout << * temp.body << endl; }
private:
T* body;
int* ref_count;
void deleteAll()
{
delete body;
body = NULL;
delete ref_count;
ref_count = NULL;
}
};