M
Mohammad
I'm implementing a c++ template class CScan to minipulate a series of
numbers.
I have implemented operator() to select a range of numbers it works
fine for an expression like:
scan1 = scan2(0, 99);
However, I have problem when I try something like:
scan1(0, 99) = scan2(100, 199);
In this case, a temporary object is created and destoryed without
copying the values to scan1. How can I accomplish this? Following is
some of the relavent code.
template<class T>
class CScan
{
public:
CScan() : m_nSize(0), m_pData(NULL)
{
}
CScan(ULONG nSize) : m_nSize(nSize)
{
m_pData = new T[m_nSize];
ZeroMemory(m_pData, m_nSize * sizeof(T));
}
CScan(T* pData, ULONG nSize) : m_nSize(nSize)
{
m_pData = new T[m_nSize];
CopyMemory(m_pData, pData, m_nSize * sizeof(T));
}
CScan(CScan& scan) : m_nSize(scan.m_nSize)
{
m_pData = new T[m_nSize];
CopyMemory(m_pData, scan.m_pData, m_nSize * sizeof(T));
}
~CScan()
{
delete [] m_pData;
}
CScan& operator=(CScan& scan)
{
if (&scan == this)
{
return *this;
}
m_nSize = scan.m_nSize;
delete [] m_pData;
m_pData = new T[m_nSize];
CopyMemory(m_pData, scan.m_pData, m_nSize * sizeof(T));
return *this;
}
ULONG Size()
{
return m_nSize;
}
T& operator[](const ULONG nIndex)
{
if (nIndex >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nIndex);
}
return m_pData[nIndex];
}
const T& operator[](const ULONG nIndex) const
{
if (nIndex >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nIndex);
}
return m_pData[nIndex];
}
CScan operator()(const ULONG nFrom, const ULONG nTo) const
{
if (nFrom >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nFrom);
}
if (nTo >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nTo);
}
ULONG nSize = nTo - nFrom + 1;
if (nSize > 0)
{
return CScan(&m_pData[nFrom], nSize);
}
else
{
return CScan;
}
}
private:
T* m_pData;
ULONG m_nSize;
};
numbers.
I have implemented operator() to select a range of numbers it works
fine for an expression like:
scan1 = scan2(0, 99);
However, I have problem when I try something like:
scan1(0, 99) = scan2(100, 199);
In this case, a temporary object is created and destoryed without
copying the values to scan1. How can I accomplish this? Following is
some of the relavent code.
template<class T>
class CScan
{
public:
CScan() : m_nSize(0), m_pData(NULL)
{
}
CScan(ULONG nSize) : m_nSize(nSize)
{
m_pData = new T[m_nSize];
ZeroMemory(m_pData, m_nSize * sizeof(T));
}
CScan(T* pData, ULONG nSize) : m_nSize(nSize)
{
m_pData = new T[m_nSize];
CopyMemory(m_pData, pData, m_nSize * sizeof(T));
}
CScan(CScan& scan) : m_nSize(scan.m_nSize)
{
m_pData = new T[m_nSize];
CopyMemory(m_pData, scan.m_pData, m_nSize * sizeof(T));
}
~CScan()
{
delete [] m_pData;
}
CScan& operator=(CScan& scan)
{
if (&scan == this)
{
return *this;
}
m_nSize = scan.m_nSize;
delete [] m_pData;
m_pData = new T[m_nSize];
CopyMemory(m_pData, scan.m_pData, m_nSize * sizeof(T));
return *this;
}
ULONG Size()
{
return m_nSize;
}
T& operator[](const ULONG nIndex)
{
if (nIndex >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nIndex);
}
return m_pData[nIndex];
}
const T& operator[](const ULONG nIndex) const
{
if (nIndex >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nIndex);
}
return m_pData[nIndex];
}
CScan operator()(const ULONG nFrom, const ULONG nTo) const
{
if (nFrom >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nFrom);
}
if (nTo >= m_nSize)
{
throw CScanError::OutsideBounds(m_nSize, nTo);
}
ULONG nSize = nTo - nFrom + 1;
if (nSize > 0)
{
return CScan(&m_pData[nFrom], nSize);
}
else
{
return CScan;
}
}
private:
T* m_pData;
ULONG m_nSize;
};