2012-11-01 2 views
0

일반 원형 버퍼에서 작업하고 있지만 복사 생성자에 대해서는 걸림돌이됩니다 (아래 코드 예제 참조).const 제네릭 객체에서 어떻게 복사합니까?

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'

나에게 준다 전체 버퍼가 복제됩니다.

무엇이 여기에 있습니까? const T 유형에서 T 유형으로 복사 할 수없는 이유는 무엇입니까?

답변

1

"C++/CLI"의 계속되는 이야기에서 또 다른 항목은 const를 지원하지 않습니다.

C++/CLI의 복사 생성자는 T (T %)입니다. 대입 연산자는 T % operator = (T %)입니다. const가없고 참조가 없습니다.

STL/CLR 구현 클래스의 본거지 인 vc/include/cliext를 사용하는 것이 좋습니다. 사용하지 마십시오.

+0

const를 사용하지 마십시오. ref를 사용하지 않거나 둘 중 하나를 사용하지 마십시오. 'const^&'는 단지'%'이어야한다고 말하는 것입니까? –

+0

참조 유형에 대해서는^%, 값 유형 또는 일반에 대해서는 %입니다. –

+0

충분히 간단합니다. 감사; 언제나처럼, 한스는 구조에 :) –

관련 문제