views:

1109

answers:

1

The interfaces in Managed C++ looka bit strange to me since they allow static methods and members inside them. For example, following is a valid MC++ interface.

interface class statinterface
{
    static int j;
    void Method1();
    void Method2();

    static void Method3()
    {
     Console::WriteLine("Inside Method 3");
    }

    static statinterface()
    {
        j = 4;
    }
};

Well, my question is that what is the use of static methods in an interface. And what happened to virtual tables etc. What will be the virtual table of the classes implementing this interface. There are lots of questions that come to mind. This type of class i.e., interface class is not equivalent to a plain abstract class since we can't have definition of non-static methods here.

I just want to know the wisdom of allowing statics in interface. This is certainly against OOP principles IMO.

A: 

The easiest way to answer this question is to use .NET Reflector to examine the assembly generated from the code.

A VTable only ever contains virtual functions, so statics simply wouldn't be included.

The language is called C++/CLI, not Managed C++ (that was something bad from way back in 2002).

This has nothing to do with OOP principles, which originally never included the concept of a pure interface anyway.

Daniel Earwicker