我有一个C ++赋值,构建我自己的简单数组列表,我应该实现这个构造函数,析构函数和一个add函数:
MyCollection.h
template<typename T>
class MyCollection
{
private: //the private members are mine
Node<T> *current;
int current_index;
int count;
void Empty();
public:
MyCollection(char * pBuffer, unsigned nBufferSize);//Constructs the array from a pre-defined buffer.
~MyCollection();
T * Add();
}
template <typename T>
struct Node
{
T value;
Node *next;
Node *previous;
};
MyCollection.cpp 我的实施
template<typename T>
MyCollection<T>::MyCollection(char * pBuffer, unsigned nBufferSize)
{
count=0;
//what to do here ???
}
template<typename T>
MyCollection<T>::~MyCollection()
{
Empty();
}
template<typename T>
T * MyCollection<T>::Add()
{
if(items>=capacity)
{
throw "Out of capacity";
}
Node<T> *dummy;
dummy = new(Node<T>);
dummy->value = new T;
if(current == NULL)
{
current=dummy;
}
else
{
current->next=dummy;
dummy->previous=current;
current=dummy;
}
count++;
return &(current->value);
}
现在,我不知道在构造函数中做了什么?这些论点是什么?
我也获得了主要部分:
int _tmain(int argc, _TCHAR* argv[]) {
if (argc < 2) {
printf("Usage:\nTest.exe buffersize (in KB)\n");
return EXIT_FAILURE;
}
unsigned nBufferSize = atoi(argv[1]) * 1024;
char * pBuffer = new char[nBufferSize];
MyCollection<CTestClass> oArray(pBuffer, nBufferSize);
//???
return 0;
}