How to implement IEnumerator<T> in C++/CLI

G

Guest

I tried but failed to implement a template class that support IEnumerator<T>
interface using C++/CLI in VS 2005 Professional version. I could not figure
out the proper syntax to implement the property "Current". The challange is
that I need to implement two versions of the property: one of type
System::Object as required by Collections::IEnumerator, the other of type T^
as required by Collections::Generic::IEnumerator<T>

template<typename T>
class A : IEnumerator<T^>
{
property System.Object^ Current {...}

property T^ Current {...}
};

the article "Explicit Override of an Interface Member" in MSDN did not help
much. Any helps are greatly appreciated.
 
J

Jay B. Harlow [MVP - Outlook]

Have you tried something like:

template<typename T>
ref class A : Generic::IEnumerator<T^>
{
private:
property System::Object^ CurrentObject
{
virtual Object^ get() sealed = IEnumerator::Current::get;
}
~A();

public:
property T^ Current
{
virtual T^ get();
}

virtual bool MoveNext(void);
virtual void Reset(void);
};


Although I would probably define it as:

template<typename T>
ref class A : Generic::IEnumerator<T>
{
private:
property System::Object^ CurrentObject
{
virtual Object^ get() sealed = IEnumerator::Current::get;
}
~A();

public:
property T Current
{
virtual T get();
}

virtual bool MoveNext(void);
virtual void Reset(void);
};


Allowing it to be used with either value or reference types.

A<String^> stringEnumerator;
A<Object^> objectEnumerator;
A<int> intEnumerator;
A<double> doubleEnumerator;


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


message |I tried but failed to implement a template class that support
IEnumerator<T>
| interface using C++/CLI in VS 2005 Professional version. I could not
figure
| out the proper syntax to implement the property "Current". The challange
is
| that I need to implement two versions of the property: one of type
| System::Object as required by Collections::IEnumerator, the other of type
T^
| as required by Collections::Generic::IEnumerator<T>
|
| template<typename T>
| class A : IEnumerator<T^>
| {
| property System.Object^ Current {...}
|
| property T^ Current {...}
| };
|
| the article "Explicit Override of an Interface Member" in MSDN did not
help
| much. Any helps are greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top