Export (0) Print
Expand All

Explicit Overrides

This topic discusses how to explicitly override a base class or interface member. A named (explicit) override should only be used to override a method with a derived method that has a different name.

function-declarator = type::function[,type::function] {
/* function definition */ 
}
function-declarator = function { /* function definition */ }

function

The function you want to override. It is possible to specify more than one function; separate function names with a comma.

function-declarator

The return type, name, and argument list of the overriding function. Note that the overriding function does not have to have the same name as the function being overridden. It is possible to specify more than one function; separate function names with a comma.

type

The base type that contains the function you want to override.

For information related to modifying the behavior of inherited types and inherited type members, see Override Specifiers.

For information about explicit overrides in native code or code compiled with /clr:oldSyntax, see Explicit Overrides (C++).

The following sample shows a simple, implicit override and implementation of a member in a base interface, not using explicit overrides.

// explicit_override_1.cpp
// compile with: /clr
interface struct I1 {
   virtual void f();
};

ref class X : public I1 {
public:
   virtual void f() {
      System::Console::WriteLine("X::f override of I1::f");
   }
};

int main() {
   I1 ^ MyI = gcnew X;
   MyI -> f();
}
X::f override of I1::f

The following sample shows how to implement all interface members with a common signature, using explicit override syntax.

// explicit_override_2.cpp
// compile with: /clr
interface struct I1 {
   virtual void f();
};

interface struct I2 {
   virtual void f();
};

ref struct X : public I1, I2 {
   virtual void f() = I1::f, I2::f {
      System::Console::WriteLine("X::f override of I1::f and I2::f");
   }
};

int main() {
   I1 ^ MyI = gcnew X;
   I2 ^ MyI2 = gcnew X;
   MyI -> f();
   MyI2 -> f();
}
X::f override of I1::f and I2::f
X::f override of I1::f and I2::f

The following sample shows how a function override can have a different name from the function it is implementing.

// explicit_override_3.cpp
// compile with: /clr
interface struct I1 {
   virtual void f();
};

ref class X : public I1 {
public:
   virtual void g() = I1::f {
      System::Console::WriteLine("X::g");
   }
};

int main() {
   I1 ^ a = gcnew X;
   a->f();
}
X::g

The following sample shows an explicit interface implementation that implements a type safe collection.

// explicit_override_4.cpp
// compile with: /clr /LD
using namespace System;
ref class R : ICloneable {
   int X;

   virtual Object^ C() sealed = ICloneable::Clone {
      return this->Clone();
   }

public:
   R() : X(0) {}
   R(int x) : X(x) {}

   virtual R^ Clone() {
      R^ r = gcnew R;
      r->X = this->X;
      return r;
   }
};

Compiler option: /clr

Community Additions

ADD
Show:
© 2014 Microsoft