hi guys :D!
im trying to make specialized template for cstring, but it seems i stuck on it..
i tried to search how to make specialized template for cstring , but there wasnt any good explanation and im trying to figure this out my self
here's my template header :
#ifndef CSTRINGVECTOR_H
#define CSTRINGVECTOR_H
#include <iostream>
template <char*>
class CstringVector
{
private:
char** aptr;
int arraySize;
void memError();
void subError();
public:
CstringVector()
{
arraySize = 0;
aptr = NULL;
}
CstringVector(int);
CstringVector(const CstringVector&);
~CstringVector();
int size() const
{
return arraySize;
}
char* getElementAt(const int);
void setSize(const int);
char* operator[](const int&);
void push_back(const char*);
void pop_back();
};
#endif
here's my source :
#include "CstringVector.h"
#include <iostream>
#include <cstdlib>
#include <new>
template <char*>
CstringVector<char*>::CstringVector(int s)
{
arraySize = s;
if(arraySize == 0)
{
aptr = NULL;
return;
}
try
{
aptr = new char[arraySize];
}
catch(std::bad_alloc)
{
memError();
}
for(int count = 0; count < arraySize; count++)
{
*(aptr + count) = 0;
}
}
template <char*>
CstringVector<char*>::CstringVector(const CstringVector& x)
{
arraySize = x.arraySize;
if(arraySize == 0)
{
aptr = NULL;
return;
}
aptr = new char[arraySize];
if(aptr == 0)
{
memError();
}
for(int count = 0; count < arraySize;count++)
{
*(aptr+count) = *(x.aptr + count);
}
}
template <char*>
CstringVector<char*>::~CstringVector()
{
delete [] aptr;
}
template <char*>
void CstringVector<char*>::memError()
{
std::cout<<"ERROR CANNOT ALLOCATE ! ENDING THE PROGRAM! ";
exit(EXIT_FAILURE);
}
template <char*>
void CstringVector<char*>::subError()
{
std::cout<<"INVALID SUBSCRIPT! ENDING PROGRAM";
exit(EXIT_FAILURE);
}
template <char*>
char* CstringVector<char*>::getElementAt(const int s)
{
if(s < 0 || s >= arraySize)
{
subError();
}
return aptr[s];
}
template <char*>
CstringVector<char*>::operator[](const int& s)
{
if(s < 0||s >= arraySize)
{
subError();
}
return aptr[s];
}
template <char*>
void CstringVector<char*>::setSize(const int s)
{
arraySize = s;
try
{
aptr = new char[arraySize];
}
catch(std::bad_alloc)
{
memError();
}
for(int count = 0; count < arraySize; count++)
{
*(aptr + count) = 0;
}
}
template <char*>
void CstringVector<char*>::push_back(const char* x)
{
char** temp = new char*[arraySize + 1];
for(int index = 0; index < arraySize; index++)
{
temp[index] = aptr[index];
}
temp[arraySize++] = x;
delete [] aptr;
aptr = temp;
}
template <char*>
void CstringVector<char*>::pop_back()
{
if(arraySize == 0 || aptr == NULL)
{
std::cout<<"cannot pop back! \n";
return;
}
char** temp = new char*[arraySize];
for(int index = 0; index < arraySize; index++)
{
temp[index] = aptr[index];
}
temp[arraySize--];
delete [] aptr;
aptr = temp;
}