我正在研究一个通用的循环缓冲区,但是在涉及到复制构造函数时遇到了一个绊脚石(请参见下面的代码示例)。
using namespace System;
/// A generic circular buffer with a fixed-size internal buffer which allows the caller to push data onto the buffer, pop data back off again and provides direct indexed access to any element.
generic<typename T>
public ref class CircularBuffer
{
protected:
array<T, 1>^ m_buffer; /// The internal buffer used to store the data.
unsigned int m_resultsInBuffer; /// A counter which records the number of results currently held in the buffer
T m_nullValue; /// The value used to represent a null value within the buffer
public:
CircularBuffer(unsigned int size, T nullValue):
m_buffer(gcnew array<T, 1>(size)),
m_nullValue(nullValue),
m_resultsInBuffer(0)
{
}
/// <summary>
/// Copy constructor
/// </summary>
CircularBuffer(const CircularBuffer^& rhs)
{
CopyObject(rhs);
}
/// <summary>
/// Assignment operator.
/// </summary>
/// <param name="objectToCopy"> The Zph2CsvConverter object to assign from. </param>
/// <returns> This Zph2CsvConverter object following the assignment. </returns>
CircularBuffer% operator=(const CircularBuffer^& objectToCopy)
{
CopyObject(objectToCopy);
return *this;
}
protected:
/// <summary>
/// Copies the member variables from a Zph2CsvConverter object to this object.
/// </summary>
/// <param name="objectToBeCopied"> The Zph2CsvConverter to be copied. </param>
void CopyObject(const CircularBuffer^& objectToBeCopied)
{
m_buffer = safe_cast<array<T, 1>^>(objectToBeCopied->m_buffer->Clone());
m_nullValue = objectToBeCopied->m_nullValue; // <-- "error C2440: '=' : cannot convert from 'const T' to 'T'"
m_resultsInBuffer = objectToBeCopied->m_resultsInBuffer;
}
};
编译这个给了我error C2440: '=' : cannot convert from 'const T' to 'T'
通常,我会将它与我自己的 ref 类一起使用,其中包括指向托管和非托管内存的指针,因此如果复制整个缓冲区,则需要深度复制缓冲区的内容。
我在这里想念什么?为什么我不能从某种类型的东西复制到某种类型const T
的东西T
?