次の方法で共有


コンパイラ エラー C2392

'method1' : 供変性の戻り型はマネージド型または WinRT 型ではサポートされていません。そうでない場合、'method2' はオーバーライドされます

共変性の戻り値の型は、Windows ランタイムのメンバー関数に対して、または /clr (共通言語ランタイムのコンパイル) オプションを使用してコンパイルする場合は、使用できません。

次の例では、C2392 を生成し、その修正方法を示しています。

// C2392.cpp
// compile with: /clr
public ref struct B {
public:
   int i;
};

public ref struct D: public B{};

public ref struct B1 {
public:
   virtual B^ mf() {
      B^ pB = gcnew B;
      pB->i = 11;
      return pB;
   }
};

public ref struct D1: public B1 {
public:
   virtual D^ mf() override {  // C2392
   // try the following line instead
   // virtual B^ mf() override {
   // return type D^ is covariant with B^, not allowed with CLR types
      D^ pD = gcnew D;
      pD->i = 12;
      return pD;
   }
};

int main() {
   B1^ pB1 = gcnew D1;
   B^ pB = pB1->mf();
   D^ pD = dynamic_cast<D^>(pB);
}