共用方式為


編譯器錯誤 C2392

'method1' :Managed 或 WinRTtypes 中不支援 covariant 傳回類型,否則會覆寫 'method2'

Windows 執行階段 成員函式或使用 /clr (Common Language Runtime 編譯) 選項進行編譯時,不允許使用 Covariant 傳回型別。

範例

下列範例會產生 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);
}