MY FIRST TEMPLATE

Im newby in C++, can you evaluate my first template code. Thanks.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//============================================================================
// Name        : templating.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

using namespace std;

#include "list_template.h"

int main() {
	TList<char> listString;

	while(true){
		for(int a=0; a<=10; a++){
			char *pChar = new char[100];
			sprintf(pChar, "ROMMEL DE TORRES %d", a);

			if( listString.addData(pChar) == false ){
				cout << "Unable to add string." << endl;
			}
		}

		while( listString.eof() == false ){
			char *pString = listString.getData();
			if(!pString) continue;

			cout << pString << endl << endl;
			pString = NULL;
			listString.next();
		}

		listString.moveFirst();

		while( listString.eof() == false ){
			char *pString = listString.getData();
			if(!pString) continue;

			cout << pString << endl << endl;
			pString = NULL;
			listString.next();
		}
		listString.clear();
		usleep(500);
	}
	return 1;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * list_template.h
 *
 *  Created on: Dec 16, 2009
 *      Author: rommel
 */

#ifndef LIST_TEMPLATE_H_
#define LIST_TEMPLATE_H_

#include <iostream>
#include <string.h>
using namespace std;

template <class T>
class TList{
public:
	TList();
	virtual ~TList();
	bool addData(T *pData);
	T    *getData();
	void next();
	void moveFirst();
	bool eof();
	void clear();
private:
	struct ListData{
		unsigned long index;
		T             *pData;
		ListData      *next;
	};

	typedef struct ListData *pListData;

	pListData head, tail;
	unsigned long index;

	/*
	 *ITERATOR
	 * */
	pListData iterator;
	bool iteratorEOF;
};

template <class T>
TList<T>::TList(){
	head        = NULL;
	tail        = NULL;
	index       = 0;
	iteratorEOF = true;
}

template <class T>
TList<T>::~TList(){
	head      = NULL;
	tail     = NULL;
	iterator = NULL;

	clear();
}

template <class T>
void TList<T>::clear(){
	pListData plist, pListNext;
	for(plist = head; plist!=NULL; plist=pListNext){
		pListNext = plist->next;

		delete plist->pData; plist->pData = NULL;
		delete plist; plist = NULL;
	}
	index       = 0;
	iteratorEOF = true;
	iterator    = NULL;
	head        = NULL;
	tail        = NULL;
	pListNext   = NULL;
}

template <class T>
bool TList<T>::eof(){
	return iteratorEOF;
}

template <class T>
void TList<T>::next(){
	if( iteratorEOF == false && (iterator->next != NULL) ){
		iterator = iterator->next;
	}else{
		iteratorEOF = true;
		iterator    = NULL;
	}
}

template <class T>
void TList<T>::moveFirst(){
	iterator = head;
	if(iterator==NULL)
		iteratorEOF = true;
	else
		iteratorEOF = false;
}

template <class T>
T *TList<T>::getData(){
	if(!iterator){
		return NULL;
	}
	return iterator->pData;
}

template <class T>
bool TList<T>::addData(T *pData){
	pListData pCurrent = new ListData;
	if(!pCurrent){
		pCurrent = NULL;
		return false;
	}

	pCurrent->index = index++;
	pCurrent->pData = pData;

	if(!head){
		iterator    = head = tail = pCurrent;
		iteratorEOF = false;
	}else{
		tail->next = pCurrent;
		tail       = pCurrent;
	}
	return true;
}

#endif /* LIST_TEMPLATE_H_ */
It's a good start, but a few comments:
1. No need for the virtual destructor. It won't be a base class.
2. You're holding pointers to dynamically allocated arrays, but you're doing a scalar delete.
3. The iterator shouldn't be part of the collection as you can only traverse it by one thing at a time.
Topic archived. No new replies allowed.