Sign in to follow this  
Followers 0
Vortez

Is it safe to use classes in containers?

9 posts in this topic

That's the question...

 

Oh and if some have the time, can they tell me if it would be safe to use classes in THIS containers i've made, a simple linked list (it work with structs):

 

(Im a bit worried about the zeromemory in the constructor and the assignment in the other...)

#ifndef _LINKED_LIST_H_
#define _LINKED_LIST_H_
#ifdef __cplusplus
//----------------------------------------------------------------------//
#include <Windows.h>
//----------------------------------------------------------------------//

// Node 
template <class DataT> class CNode {
public:
	DataT Data;
	CNode<DataT> *Next;
	CNode<DataT> *Prev;
	CNode(){
		ZeroMemory(&Data, sizeof(DataT));
		Next = NULL;
		Prev = NULL;
	}
	CNode(DataT d){
		Data = d;
		Next = NULL;
		Prev = NULL;
	}

	CNode<DataT> *GetNext(){return Next;}
	CNode<DataT> *GetPrev(){return Prev;}
};

//----------------------------------------------------------------------//

// LinkedList 
template <class DataT> class CLinkedList : public CNode<DataT> {	
private:
	CNode<DataT> *First, *Last;
	UINT NumNodesAllocated;
public:
	CLinkedList(){
		First = NULL;
		Last  = NULL;
		NumNodesAllocated = 0;
		srand(GetTickCount());
	}
	~CLinkedList(){
		Clear();
	}
	 
	UINT GetNodesCount();

	CNode<DataT> *GetNode(UINT Indx);
	CNode<DataT> *GetFirstNode();
	CNode<DataT> *GetLastNode();

	CNode<DataT>* Push(DataT *d);
	bool Pop(DataT *d);
	bool Delete(CNode<DataT> *pNode);
	void Clear();

	void Randomize();

	void Fill(CNode<DataT> *pNode);
};

template <class DataT> UINT CLinkedList<DataT>::GetNodesCount()
{
	return NumNodesAllocated;
}

template <class DataT> CNode<DataT>* CLinkedList<DataT>::GetNode(UINT Indx)
{
	CNode<DataT> *pNode = First;
	if(!pNode){return NULL;}
	if(Indx >= NumNodesAllocated){return NULL;}

	UINT i = 0;
	while(i < Indx){
		pNode = pNode->GetNext();
		if(!pNode){return NULL;}
		i++;
	}

	return pNode;
}

template <class DataT> CNode<DataT>* CLinkedList<DataT>::GetFirstNode()
{
	return First;
}

template <class DataT> CNode<DataT>* CLinkedList<DataT>::GetLastNode()
{
	return Last;
}

template <class DataT> CNode<DataT>* CLinkedList<DataT>::Push(DataT *d)
{
	CNode<DataT> *pNewNode = new CNode<DataT>;
	NumNodesAllocated++;

	pNewNode->Data = *d;
	if(First == NULL){
		Last  = pNewNode;
		First = pNewNode;
    } else {
		pNewNode->Prev = Last;
		Last->Next = pNewNode;
		Last = pNewNode;
    }

	return pNewNode;
}

template <class DataT> bool CLinkedList<DataT>::Pop(DataT *d)
{
	if(Last){
		if(d != NULL)
			*d = Last->Data;
		return Delete(Last);
	}

	return false;
}

template <class DataT> bool CLinkedList<DataT>::Delete(CNode<DataT> *pNode)
{
	if(pNode){
		if(pNode->Prev){
			pNode->Prev->Next = pNode->Next;
			if(pNode->Next){
				pNode->Next->Prev = pNode->Prev;
			} else {
				Last = pNode->Prev;
			}
		} else {
			if(pNode->Next){
				pNode->Next->Prev = NULL;
				First = pNode->Next;
			} else {
				First = NULL;
				Last  = NULL;
			}
		}

		delete pNode;
		NumNodesAllocated--;

		return true;
	}

	return false;
}

template <class DataT> void CLinkedList<DataT>::Clear()
{
	while(Pop(NULL));
}

template <class DataT> void CLinkedList<DataT>::Randomize()
{
	CLinkedList<DataT> TmpList;

	UINT NumItems = this->GetNodesCount();

	while(NumItems > 0){
	
		UINT RandVal = rand() % NumItems;
	
		CNode<DataT> *pNode = this->GetNode(RandVal);
		
		DataT d = pNode->Data;
		TmpList.Push(&d);

		this->Delete(pNode);

		NumItems--;
	}
	this->Clear();

	for(UINT Cpt = 0; Cpt < TmpList.GetNodesCount(); Cpt++){

		CNode<DataT> *pNode = TmpList.GetNode(Cpt);

		DataT d = pNode->Data;
		this->Push(&d);
	}

	TmpList.Clear();
}

template <class DataT> void CLinkedList<DataT>::Fill(CNode<DataT> *pNode)
{
	this->Clear();
	
	while(pNode){
		this->Push(&pNode->Data);
		pNode = pNode->GetNext();
	}
}

#endif
#endif //--_LINKED_LIST_H_

Thx.

Edited by Vortez
0

Share this post


Link to post
Share on other sites

Hey, thx for the quick answer. Just to be sure

 

(const DataT *d)

 

is the same as (const DataT& d)

 

right?

0

Share this post


Link to post
Share on other sites

Yes, all the std:: containers work well with any data type.

That's not quite right. In order to work with a standard library container a class needs to fulfill certain requirements. Under the previous version of the standard every class pretty much needed to satisfy the copy constructable and assignable requirements, but with the current version the requirements vary by container and by container operation. For instance if a class doesn't have an accessible destructor you can't stick it in a container. Nor can you have a container that stores abstract base classes by value.
4

Share this post


Link to post
Share on other sites

Yes, it's safe to use classes in the standard library containers. Some standard library containers requires the class to have certain operations (like operator < for sortable containers like std::map, or operator ==, or copy constructors, etc...) to be defined, but will thankfully refuse to compile if that specific container's the required operators aren't defined, so they are safe to use.

 

About this time last year I was designing a custom container and running into alot of questions about proper allocation and copying and things like that - you might find the resulting thread useful.

1

Share this post


Link to post
Share on other sites

Turn out i decided to use my other linked list class for now, which is not templatized. I have to rewrite some parts there and there each time i use it but in return i know it work

perfectly and let me do more advanced stuffs if needed. (Still, im gonna keep that page bookmarked to fix it later, thx to HodMan for the inputs)

 

L. Spiro ---> So, if i understand correctly, a reference is like a read-only pointer, that cannot be modified or point to NULL, am i right?

Edited by Vortez
0

Share this post


Link to post
Share on other sites

L. Spiro ---> So, if i understand correctly, a reference is like a read-only pointer, that cannot be modified or point to NULL, am i right?

It is only read-only if it is const.


L. Spiro
1

Share this post


Link to post
Share on other sites
Afaik you can't tell the difference between a pointer and a reference once you debug the machine code in an assembler. Both are 'pointers' in there.

References are more like a compile-time pointer that disallow referring to invalid objects (ie NULL).
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0