所以,我是C++(全新)的新手,作为一项任务,我必须编写一个充当数组包装器的类。由于我太新了,我不确定我是否超负荷运营商和这样的工作,所以如果有人能这么好心地告诉我,我的代码的外观:C++中的数组包装类
这将是头:
class MyArray
{
private:
string* sList;
unsigned int size;
public:
MyArray(const unsigned int size = 1);
MyArray(const MyArray &toCopy);
MyArray& operator=(const MyArray& toAssign);
~MyArray();
//MyArray& operator+
string& operator[](const int index);
const int size();
};
,这将是底层的代码:
MyArray::MyArray(const unsigned int initSize)
: size(initSize)
{
sList = new string[initSize];
return;
}
MyArray::MyArray(const MyArray &toCopy)
: size(toCopy.size)
{
if(toCopy.sList)
{
sList = new string[size];
for(int a=0; a<size; a++){
strcpy(sList[a], toCopy.sList[a]);
}
}
else sList = NULL;
return;
}
MyArray& operator=(const MyArray& toAssign)
{
if(this != &toAssign)
{
if(sList)
{
delete [] sList;
}
size = toAssign.size;
if (toAssign.sList)
{
sList = new string[size];
for(int a=0; a<size; a++){
strcpy(sList[a], toCopy.sList[a]);
}
}
else
{
sList = NULL
}
}
}
MyArray::~MyArray()
{
delete [] sList;
return;
}
string& MyArray::operator[](const int index)
{
return sList[index];
}
const int MyArray::size()
{
return this.size;
}
运营商+功能仍然需要写的,但我要确保我有什么有意义我再继续。
你也许应该测试,并寻求帮助,让您知道具体是什么,我们可以帮助您与之前运行了几次。 – 2013-04-22 23:28:10
将你的要求写成单元测试,然后你会知道你的操作员是否工作。 – 2013-04-22 23:28:39
使用调试器进行分步 - 您将看到它的工作原理。 – Rob 2013-04-22 23:30:07