NS_OVERRIDE

NS_OVERRIDE is a macro which allows C++ code in Mozilla to specify that a method is intended to override a base class method. If there is no base class method with the same signature, a compiler with static-checking enabled will fail to compile.

NS_OVERRIDE is declared in nscore.h, beginning with Gecko 2.0. It was introduced in bug 500870.

Example

Class A has a method GetFoo() which is overridden by class B:

class A
{
  virtual nsresult GetFoo(nsIFoo** aResult);
};

class B : public A
{
  NS_OVERRIDE virtual nsresult GetFoo(nsIFoo** aResult);
};

Later, the signature of A::GetFoo() is changed to remove the output parameter:

 class A
 {
-  virtual nsresult GetFoo(nsIFoo** aResult);
+  virtual already_AddRefed<nsIFoo> GetFoo();
 };

B::GetFoo() no longer overrides A::GetFoo() as was originally intended. A compiler with static-checking enabled will issue the following error:

test.cpp:8: error: NS_OVERRIDE function B::GetFoo(nsIFoo**) does not override a base class method with the same name and signature.

Document Tags and Contributors

 Contributors to this page: teoli, trevorh, Sheppy, BenjaminSmedberg
 Last updated by: trevorh,