Question about interface method and implementation

Hi All,

I'm trying the Visual Studio 2005 Beta 2, especially for C++/CLI implementation
in VC++.  I was wondering if someone could please help me with a question
that I have. Any suggestions would be greatly appreciated.

I've defined an interface like this:

public interface class IMyInterface
{
    void TryAndTry();
};

and a class implementing this interface

ref class MyClass: IMyInterface
{
public:
    void TryAndTry()
    {
        Console::WriteLine("DONE");
    }

};

I've a test program that simply creates an instance of this class. When I compile this
project, I get following compilation error:

"error C3766: 'MyClass' must provide an implementation for the interface method 'void IMyInterface::TryAndTry(void)'   "

I'm surely missing something here as I clearly have provided the implementation
for the interface's method and still the compiler is complaining. Any ideas

Thanks,
Bhavin
  



Answer this question

Question about interface method and implementation

  • etncc

    Thanks Jonathan, that solves the problem.. However, I'm not quite
    sure about the need for using virtual in method implementation. If we
    go by pure C++ way, shouldn't virtual be used in the interface definition
    rather in the classes that implement it That is the way I'd have done
    it by creating an abstract class in C++ by declaring a pure virtual
    method in the class and providing implementation down in the
    hieararchy..Sorry if this is too naive.

    Best,
    Bhavin


  • KevinBurton

    In your interface defininition you're requesting that base classes implement a private method named "TryAndTry" that returns void and takes no parameters.

    In your base class you are defining a public method named "TryAndTry" that returns void and takes no parameters.



  • PavanM

    Peter: interfaces are like structs: all the members are public by default. The problem here is that in C++/CLI there is not such thing as an implicitly virtual method. The fix, I suspect, is to change the definition of the ref class to:

    ref class MyClass : IMyInterface
    {
    public:
        virtual void TryAndTry()
        {
            Console::WriteLine("DONE");
        }
    };



  • Stevo Guy

    In regular C++ the method in the class is virtual even though you haven't marked it as virtual. Consider the following case:

    struct IDoSomething {
       virtual void vmf() = 0;
    };

    class A : public IDoSomething {
    public:
       void vmf() {
       }
    };


    In this case as A::vmf overrides IDoSomething::vmf() it is implicitly marked as virtual. The definition of class A above is identical to:

    class A : public IDoSomething {
    public:
       virtual void vmf() {
       }
    };


    The C++/CLR rules are stricter: mostly because the CLR allows (but Visual C++ does not support - mostly because it is too weird) the following:

    ref interface IDoSomething {
       void vmf();
    };

    ref class A : IDoSomething {
    public:
       void vmf() {
       }
    };


    In this case the non-virtual method A::vmf could be used to implement the interface method IDoSomething::vmf: but as I said don't try this at home.

    So for this reason and for the reasons I mention in my earlier posts C++/CLI does not support implicitly virtual methods. Doing so, even though it requires slightly more typing, does lead to much clearer code. I hate having to walk up a class hierarchy just to check if a method is virtual or not Smile.

  • Question about interface method and implementation