Hi
I am creating my own Queue class to learn about Queues and pointers.
I have come across a question of two styles and I don't know if there
are any dangers associated with them.
I coded my remove function as follows
template <class T>
bool adsQ<T>::Remove (T* theData)
{
Qnode<T>* removeNode = 0;
if (true == isEmpty())
{
//cout << "False" << endl;
return false;
}
else
{
/* What I am trying to achive here is to change the data pointed to
by theData to the data pointed to by pData.*/
*theData =*(pHead->pData); //Data exchanged
removeNode = pHead;
if (count == 1)
{
pHead = 0; //Pointers to Null
pTail = 0;
}
else
{
pHead = pHead->nextQnode;
}
count--;
/*I then free up the memory which contained the pHead, pData.
Knowing that *theData now contains the same data as pData did*/
delete removeNode;
removeNode = 0; //Make pointer safe
//cout << "True" << endl;
return true;
}
}
I then read about pointers to pointers and amended my function signature
template <class T>
bool adsQ<T>::Remove (T** theData) ...
and the line where the data is exchanged.
*theData = pHead->pData;
And called the function thus
pQ->Remove(&pD);
I now appear to get the same results from both, the returning of the
data contained in the pointer pData outwith the function.
However in changing the function to take a pointer to pointer am I
storing up trouble for myself in that as the free space pointed to by
pData is now pointed at by pD outwith the function (is this right?) and
also pData within the function, can I now delete it within the function
(through removeNode) safely or is this trouble as I appear to be
deleting memory pointed at by two pointers.
TYIA
nifsmith