Home >Backend Development >C++ >What Does the `new` Keyword Mean in a Method Signature?
New Keyword in Method Signature
Question: What does the new keyword signify in a method signature?
Answer: In a method signature, the new keyword indicates that the method is a new method in the derived class that hides the base class implementation of a method. It is different from the override keyword, which can only be used to override virtual methods declared in the base class.
Example:
Consider the following base class:
public class Base { public void BaseMethod() {} }
Now, let's create a derived class that overrides the BaseMethod method using the override keyword:
public class Derived : Base { public override void BaseMethod() {} }
In this example, the override keyword tells the compiler that the BaseMethod method in the derived class is overriding the base class implementation.
However, let's now create a different derived class and use the new keyword in the method signature:
public class AnotherDerived : Base { public new void BaseMethod() {} }
In the above example, the new keyword is used because the BaseMethod method in the derived class is not overriding the base class implementation. Instead, it is creating a new method with the same name as the base class method.
Key Differences between new and override:
The above is the detailed content of What Does the `new` Keyword Mean in a Method Signature?. For more information, please follow other related articles on the PHP Chinese website!