インタフェース

C++/CLIだとインタフェースの明示的実装に制限がありそ。それともバグかしら?
しかし、インタフェースの明示的実装周りはWorking Draft1.9(2005/1)と結構変わっています。まだ、言語仕様固まってないのかなぁ・・・

#include "stdafx.h"

using namespace System;

ref class Foo {
};

interface class IFoo {
    void f();
    void g();
    void h();
};

interface class IBar {
    void f();
};

interface struct IBaz {
    void h();
};

// C#では継承元のクラスは先頭に書かなければならなかったが、
// C++/CLIにはこの制限は無い模様
ref class Qux : IBar, Foo {
public:
    // IBarを実装
    virtual void f() {}
};

ref struct Quux : Foo, IBar, IBaz {
    // IBarの実装
    virtual void f() {}
private:
    // IBaz::hの明示的実装
    virtual void h() sealed = IBaz::h {}
};

#if 0
// C++/CLIだと同一名称の関数をインタフェースによる明示的実装で含めること出来ない?
// error C2535: 'void Quuuxf(void)':メンバ関数は、既に定義または宣言されています。
ref struct Quuux : IFoo, IBar {
    virtual void g() {}
    virtual void h() {}
    virtual void f() {}
private:
    // IBar::fの明示的実装
    virtual void f() sealed = IBar::f {}
};

// C#なら出来るんだけどなぁ・・・
class Quuux : IFoo, IBar {
    public void g() {}
    public void h() {}
    public void f() {}
    void IBar.f() {}
}

#endif

int main(array<System::String ^> ^args)
{
    Quux^ q = gcnew Quux();
    IBar^ bar = q;
    IBaz^ baz = q;

    q->f();
    bar->f();
    // メンバ関数hはIBaz経由でなければ呼べない
    // q->h();
    baz->h();

    return 0;
}