Re: Template trouble!!!
Salvatore Di Fazio wrote:
Hi guys,
I made the following template:
template <class RECORD, int iSize>
class CArrayTable
{
public:
CArrayTable();
~CArrayTable();
// ...
};
template <class RECORD, int ISIZE>
CArrayTable<RECORD, iSize>::CArrayTable()
{
m_iCount = ISIZE;
m_iIndex = 0;
m_pRECORD = new RECORD[iElement];
}
First a couple of style issues (which you can choose to ignore).
1. Don't begin your class names with a prefix C. They are not MFC
classes.
2. m_ prefix for class members is not so bad but otherwise avoid the
Hungarian notation. So m_index is better than m_iIndex.
Now some issues you can't ignore:
3. C++ is case-sensitive so ISIZE as the template parameter must remain
all uppercase one line below.
4. iElement is undefined.
5. If you allocate an array with new in the constructor then the
destructor must be defined (it is declared here but not defined) and it
must call delete[]. Plus you must either handle assignment and
copy-construction or disable them by declaring them as private. If you
use vector you can avoid.
6. What is ISIZE used for anyway? Is it meant to be a fixed-size array?
If so why are you using new at all?
7. What is m_iCount used for? If the size of the array remains constant
throughout, you can use ISIZE throughout your class whenever you need
to know it.
8. What is iIndex used for? A collection should generally not hold its
own position. Instead you have iterators pointing to locations in a
collection.