Hi
I have got a problem with destructor calls.
I have a class which implements a linked list like this:
template <class NODETYPE>
class List
{
public:
List();
~List();
void vdInsertFront(const NODETYPE &);
void vdInsertLast(const NODETYPE &);
int inDeleteFront(void);
int inDeleteLast(void);
bool blIsEmpty();
void vdPrint();
int vdShuffle(void);
int inGetLenght();
NODETYPE * inObtenerDatoNodo(int);
private:
int inLenght;
Node<NODETYPE> *ptrFirst;
Node<NODETYPE> *ptrLast;
Node<NODETYPE> *ptrNewNode(const NODETYPE &);
};
This is its constructor and destructor
template <class NODETYPE>
List<NODETYPE>::List()
{
ptrFirst = 0;
ptrLast = 0;
}
template <class NODETYPE>
List<NODETYPE>::~List()
{
if(!blEmpty())
{
Nodo<NODETYPE> * ptrActual = ptrFirst;
Nodo<NODETYPE> * ptrAux;
while(ptrActual != 0)
{
ptrAux = ptrActual;
ptrActual = ptrActual->ptrNext;
delete ptrAux;
}
}
}
.
.
.
This function returns a pointer to an especific element of the list
template <class NODETYPE>
NODETYPE * List<NODETYPE>::inGetNodeData(int index)
{
Nodo<NODETYPE> * ptrAux = ptrFirst;
for(int i = 0; i <= index - 1; i++)
ptrAux = ptrAux->ptrNext;
return &ptrAux->_data;
}
Now I have another class that has a list object as a private member and
a function that returns a pointer of the list
class MyClass
{
public:
List<int> * lstGetList1(void);
private:
List<int> lstList;
};
List<int> * foo::lstGetList1(void)
{
return &lstList;
}
Now, if I call the function vdPrint of the member list in a foo object like this:
MyClass foo;
(foo.lstGetList1())->vdPrint();
The destructor is called, deleting my list when I don't want to do that.
The code in this example is very simplified of what I have. In fact I'm creating a list of MyClass objects and the the code is more or less like this:
List<MyClass> foo;
((foo.inGetNodeData(1))->lstGetList1())->vdPrint();
I think the destructor its been called because Im creating an "anonymous" list object, calling vdPrint method, an then, because the next line code is beyond the anonymous list scope, the destructor is called automatically. How can I avoid this?
Thanks in advance
Ivan