所以,我是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