Reputation: 37458
I'm working on a generic circular buffer but have hit a stumbling block when it comes to the copy constructor (see the code example below).
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;
}
};
Compiling this gives me error C2440: '=' : cannot convert from 'const T' to 'T'
Typically I'd be using this with my own ref classes which include pointers to managed and unmanaged memory so the contents of the buffer would need to be deep copied if the entire buffer is duplicated.
What am I missing here? Why can't I copy from something of type const T
to something of type T
?
Upvotes: 0
Views: 199
Reputation: 941337
Another entry in the continuing saga of "C++/CLI doesn't really support const".
The copy constructor in C++/CLI is T(T%). The assignment operator is T% operator=(T%). No const and no ref.
A good place to see them used is vc/include/cliext, home of the STL/CLR implementation classes. Don't use them.
Upvotes: 1