VC类模板的怪问题
template<typenameT,int_size>classDynamic1Dim{public:Dynamic1Dim();~Dynamic1Dim();T&op...
template<typename T,int _size>
class Dynamic1Dim
{
public:
Dynamic1Dim();
~Dynamic1Dim();
T& operator[](int index);
protected:
public:
T* m_pBuf;
};
template<typename T,int _size>
Dynamic1Dim<T,_size>::Dynamic1Dim()
{
m_pBuf = new T[_size];
}
template<typename T,int _size>
T& Dynamic1Dim<T,_size>::operator [](int index)
{
return m_pBuf [index];
}
template<typename T,int _size>
Dynamic1Dim<T,_size>::~Dynamic1Dim()
{
for(int i = 0; i < _size; i++)
{
m_pBuf[i].~T();
}
//1* delete[] m_pBuf;
}
template<typename T,int _m,int _n>
class Dynamic2Dim
{
public:
Dynamic2Dim();
~Dynamic2Dim();
Dynamic1Dim<T,_n>& operator[](int index);
protected:
public:
Dynamic1Dim<T,_n>* m_pBuf;
};
template<typename T,int _m,int _n>
Dynamic2Dim<T,_m,_n>::Dynamic2Dim()
{
m_pBuf = new Dynamic1Dim<T,_n>[_m];
}
template<typename T,int _m,int _n>
Dynamic1Dim<T,_n>& Dynamic2Dim<T,_m,_n>::operator [](int index)
{
return m_pBuf [index];
}
template<typename T,int _m,int _n>
Dynamic2Dim<T,_m,_n>::~Dynamic2Dim()
{
for(int i = 0; i < _m; i++)
{
m_pBuf[i].~Dynamic1Dim();
}
//2* delete[] m_Buf;
}
测试代码:
//(1)
Dynamic1Dim<int ,5> _TD1;
int a ;
for (int r0=0;r0<5;r0++)
{_TD1[3] = r0+1;
a = _TD1[3];}
//(2) Dynamic2Dim<int,5,5> _TD2;
for (int i0=0;i0<5;i0++)
{
for (int j0=0;j0<5;j0++)
{
_TD2[i0][j0] = i0+j0+i0*j0;
a = _TD2[i0][j0];
}
}
//(3)
Dynamic1Dim<Dynamic1Dim<int ,5>,5> _TD3;
for (int i00=0;i00<5;i00++)
{
for (int j00=0;j00<5;j00++)
{
_TD3[i00][j00] = i00+j00+i00*j00;
a = _TD2[i00][j00];
}
}
当//1*处有效时代码//(1)可正确执行,没有错误。代码//(3)出错,好像是在析构时出的错误。
当//2*处有效时代码//(2)不能正确执行,也好像是析构时出错。
编译环境VC6.0 展开
class Dynamic1Dim
{
public:
Dynamic1Dim();
~Dynamic1Dim();
T& operator[](int index);
protected:
public:
T* m_pBuf;
};
template<typename T,int _size>
Dynamic1Dim<T,_size>::Dynamic1Dim()
{
m_pBuf = new T[_size];
}
template<typename T,int _size>
T& Dynamic1Dim<T,_size>::operator [](int index)
{
return m_pBuf [index];
}
template<typename T,int _size>
Dynamic1Dim<T,_size>::~Dynamic1Dim()
{
for(int i = 0; i < _size; i++)
{
m_pBuf[i].~T();
}
//1* delete[] m_pBuf;
}
template<typename T,int _m,int _n>
class Dynamic2Dim
{
public:
Dynamic2Dim();
~Dynamic2Dim();
Dynamic1Dim<T,_n>& operator[](int index);
protected:
public:
Dynamic1Dim<T,_n>* m_pBuf;
};
template<typename T,int _m,int _n>
Dynamic2Dim<T,_m,_n>::Dynamic2Dim()
{
m_pBuf = new Dynamic1Dim<T,_n>[_m];
}
template<typename T,int _m,int _n>
Dynamic1Dim<T,_n>& Dynamic2Dim<T,_m,_n>::operator [](int index)
{
return m_pBuf [index];
}
template<typename T,int _m,int _n>
Dynamic2Dim<T,_m,_n>::~Dynamic2Dim()
{
for(int i = 0; i < _m; i++)
{
m_pBuf[i].~Dynamic1Dim();
}
//2* delete[] m_Buf;
}
测试代码:
//(1)
Dynamic1Dim<int ,5> _TD1;
int a ;
for (int r0=0;r0<5;r0++)
{_TD1[3] = r0+1;
a = _TD1[3];}
//(2) Dynamic2Dim<int,5,5> _TD2;
for (int i0=0;i0<5;i0++)
{
for (int j0=0;j0<5;j0++)
{
_TD2[i0][j0] = i0+j0+i0*j0;
a = _TD2[i0][j0];
}
}
//(3)
Dynamic1Dim<Dynamic1Dim<int ,5>,5> _TD3;
for (int i00=0;i00<5;i00++)
{
for (int j00=0;j00<5;j00++)
{
_TD3[i00][j00] = i00+j00+i00*j00;
a = _TD2[i00][j00];
}
}
当//1*处有效时代码//(1)可正确执行,没有错误。代码//(3)出错,好像是在析构时出的错误。
当//2*处有效时代码//(2)不能正确执行,也好像是析构时出错。
编译环境VC6.0 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询