以下编译通过:
以下编译错误:
现在修改代码,正确通过测试。
扩展应用 泛型Object.ToString()。
/// <summary>
/// 转换为字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// 创建时间: 2022-12-18 最后一次修改时间:2022-12-20
template<class T>
_string DList<T>::ToString()const
{
_string sResult;
if ( typeid(T) == typeid(int) )
{
if (_Count == 0) return sResult;
if (_Count == 1)
{
sResult.add(_string::java_valueOf(*( (int*) (& (_First->Data)))));
}
DListNote<T>* dn = this->_First;
while (dn != _Last)
{
int* pInt = (int*)&(dn->Data);
sResult.add(_string::java_valueOf(*pInt));
sResult.add(_t(','));
dn = dn->Next;
}
sResult.add(_string::java_valueOf(*((int*)(&(_Last->Data)))));
}
else if (typeid(T) == typeid(double))
{
if (_Count == 0) return sResult;
double* pd;
if (_Count == 1)
{
pd = (double*)(&(_First->Data));
sResult.add(_tostr(*pd));
}
DListNote<T>* dn = this->_First;
while (dn != _Last)
{
pd = (double*)&(dn->Data);
sResult.add(_tostr(*pd));
sResult.add(_t(','));
dn = dn->Next;
}
pd = (double*)&(_Last->Data);
sResult.add(_tostr(*pd));
}
else if (typeid(T) == typeid(_string))
{
if (_Count == 0) return sResult;
_string* ps;
if (_Count == 1)
{
ps = (_string*)(&_First->Data);
sResult.add(*ps);
}
DListNote<T>* dn = this->_First;
while (dn != _Last)
{
ps = (_string*)(&dn->Data);
sResult.add(*ps);
sResult.add(_t(','));
dn = dn->Next;
}
ps = (_string*)(&_Last->Data);
sResult.add(*ps);
}
else if(typeid(T) == typeid(Object_))
{
if (_Count == 0) return sResult;
Object_* po;
if (_Count == 1)
{
po = (Object_*)(&_First->Data);
sResult.add((_string) (*po) );
}
DListNote<T>* dn = this->_First;
while (dn != _Last)
{
po = (Object_*)(&dn->Data);
sResult.add((_string)(*po));
sResult.add(_t(','));
dn = dn->Next;
}
po = (Object_*)(&_Last->Data);
sResult.add((_string)(*po));
}
else
{
sResult.add(_t("数据类型是:"));
sResult.add(gs.s_UTF8String_to_TextW(typeid(T).name()));
sResult.add(_t(",函数还未完成,或者你自己重写 ToString() 虚拟函数 !"));
}
return sResult;
}