tags:

views:

47

answers:

2

I want to port the following code from c# to c++/cli:

class MyClass : IEnumerable<int> { ... }

I've tried

class ref class MyClass : IEnumerable<int>

but it doesn't seem to be working.

+1  A: 

C++ has multiple types of inheritance. Don't forget to specify it, e.g.:

ref class MyClass : public IEnumerable<int>
{ };

In C++/CLI, I frequently find myself spelling out the full namespace in interface implementations. E.g.:

ref class MyClass :
    public MyCompany::MyProject::MyComponent::IMyInterface
{ };

If your class doesn't actually implement the provided interface, you'll also (of course) get an error. And you'll want to remove the ^ from the class declaration. You're inheriting from an interface, not from a GC Handle to an instance of that interface.

Greg D
Yeah, it was System::Collections::Generic instead of System::Collections, but anyway I'll give you the points as what you said is useful for anyone searching about the topic.
devoured elysium
Whatever the problem was, this isn't it. In C++/CLI, `public` is the only supported inheritance visibility qualifier, and it is also the default one, so `class MyClass : IFoo` and `class MyClass : public IFoo` are precisely equivalent.
Pavel Minaev
+1  A: 

Assuming your code is exactly what you're trying to compile, you have an extra class there. It should be just ref class, and not class ref class. Also, don't forget to translate any C# using statements to C++ using namespace, i.e.: using namespace System::Collections::Generic.

Pavel Minaev