This documentation is archived and is not being maintained.

Compiler Error C2392

'method1' : covariant returns types are not supported in managed types, otherwise 'method2' would be overridden

Covariant return types are not allowed when compiling with the /clr (Common Language Runtime Compilation) option.

The following sample generates 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);
}

The following sample generates C2392.

// C2392_2.cpp
// compile with: /clr:oldSyntax

public __gc struct B {
   int i;
};

public __gc struct D : public B {};

public __gc struct B1 {
   virtual B* mf() {
      B* pB= new B;
      pB->i = 11;
      return pB;
   }
};

public __gc struct D1 : public B1 {
   virtual D* mf() {   // C2392
   // try the following line instead
   // virtual B* mf() {
   // return type D* is covariant with B*, not allowed with C:R types
      D * pD = new D();
      pD->i = 12;
      return pD;
   }
};

int main() {
   B1 *pB1 = new D1;
   B * pB = pB1->mf();
   D * pD = dynamic_cast<D *>(pB);
}
Show: