【发布时间】:2016-08-05 02:08:13
【问题描述】:
在我的库中,我有一个数组类:
template < class Type >
class Array
{
Type* array_cData;
...
Type& operator[] (llint Index)
{
if (Index >= 0 && Index < array_iCount && Exist())
return array_cData[Index];
}
};
这很好,但是如果我在堆栈中生成了这样的类:
Array<NString>* space = new Array<NString>(strList->toArray());
checkup("NString split", (*space)[0] == "Hello" && (*space)[1] == "world");
//I must get the object pointed by space and after use the operator[]
所以我的问题是:我可以在 array_cData 中获取对象,而无需像这样指定对象:
Array<NString>* space = new Array<NString>(strList->toArray());
checkup("NString split", space[0] == "Hello" && space[1] == "world");
提前致谢! :3
-Nobel3D
【问题讨论】:
-
当然,只需使用自动变量:
Array<NString> space(strList->toArray());。更好的是使用std::array。 -
@Jarod42 strList->toArray() 返回一个 Array
,我知道函数返回 Array * 会更好,我想改进 -Nobel3D -
它返回
Array<NString>似乎比通过指针更好,但是你需要指针吗? -
是的,当用户调用操作员时,我只想调用 'space[0]',而 'space' 是一个指针 -Nobel3D
标签: c++ arrays c++11 casting operator-overloading