2010-02-12 3 views
2

데이터 스트림이 System::Collections::Queue에 포함되어 있습니다. 내 데이터 소스는 동일한 데이터를 여러 스트림에 출력 할 수 있지만 이렇게하려면 각 데이터에 대해 데이터를 복제해야합니다. 저는 현재 다음을 수행 : 나는 MyData 객체를 보내고있다으로 잘 한 일C++에서 익명으로 개체를 복제하는 방법 - CLI?

void DataGatherer::AddMyDataToQueues(MyData^ data) 
{ 
    // Send duplicates to all queues 
    for(int i = 0; i < m_outputQueues->Count; i++) 
    { 
     AddResultToQueue(gcnew MyData(data), (Queue^)m_outputQueues[i]); 
    } 
} 

. 그래도 내가 MyOtherData 개체를 보내고 싶다고하자. 이 같은 더 일반적인 뭔가 할 좋은 것 :

void DataGatherer::AddDataToQueues(Object^ obj) 
{ 
    // Send duplicates to all queues 
    for(int i = 0; i < m_outputQueues->Count; i++) 
    { 
     AddResultToQueue(gcnew Object(obj), (Queue^)m_outputQueues[i]); 
    } 
} 

을 ...하지만 컴파일되지 않습니다 때문에 :

1>.\DataGatherer.cpp(72) : error C3673: 'System::Object' : class does not have a copy-constructor 

그래서 그것의 유형을 알고 있기없이 개체를 복제 할 수 있습니까? .. 그렇다면 어떻게해야합니까? :)

답변

1

MyDataMyOtherData 모두에 ICloneable을 구현 한 다음 AddDataToQueues을 변경하여 ICloneable을 구현하는 모든 개체를 허용합니다.

public ref class MyOtherData : public ICloneable 
{ 
public: 
    MyOtherData() 
     : m_dummy(-1) 
    { 
    } 

    virtual Object^ Clone() 
    { 
     MyOtherData ^clone = gcnew MyOtherData(); 
     clone->m_dummy = m_dummy; 
     return clone; 
    } 

private: 
    int m_dummy; 
}; 

하고 ...

void DataGatherer::AddDataToQueues(ICloneable^ data) 
{ 
    // Send duplicates to all queues 
    for(int i = 0; i < m_outputQueues->Count; i++) 
    { 
     AddResultToQueue(data->Clone(), (Queue^)m_outputQueues[i]); 
    } 
} 
+0

좋아 보인다; 나는 그것을 줄 것이다 :-) –