sof
sof

Reputation: 9649

Are virtual functions only overridable in C++?

In C#, a virtual method of base class can be either overridden or hidden by its derived classes, e.g.

using System;

class A {
 public virtual void M() {
  System.Console.WriteLine("A");
 }
}

class B: A {
 public override void M() {
  System.Console.WriteLine("B");
 }
}

class C: A {
 public new void M() {
  System.Console.WriteLine("C");
 }
}

class P {
 static void Main(string[] args) {
  A b = new B();
  b.M();
  A a = new C();
  a.M();
 }
}

Output:

B
A

How about in C++? Only overridable?

Upvotes: 0

Views: 860

Answers (4)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133072

[Update for C++11]

Since C++11, you can use the override keyword to tell the compiler that you're intending to override the base class virtual function.

void foo() override;

If there is no override keyword but the base class contains a virtual function with the same* signature, then the function is still overriden. Otherwise, hidden.

[Original answer for C++03]

Of course, you can both override and hide a virtual function in C++. You just aren't very explicit about it (not in C++03 anyway).

example

class A
{
public:
   virtual int f(char);
};

class B : public A
{
public:
    virtual int f(); //hides A::f
};

class C : public A
{
public:
    virtual int f(char); //overrides A::f
};

If the signature coincides with the Base virtual method* then it's overriding. Otherwise it's hiding.

So sometimes you accidentally forget a const and end up hiding the function instead of overriding it. The C++11 override attribute is called to solve this problem. If you use this override attribute but the signatures are incompatible, you will get a compiler error.

*(or differs only in that the return types are pointers or references to a Base class and a publicly Derived class respectively)

Upvotes: 5

Masa
Masa

Reputation: 311

Here, what is happening in your code: When you write b.M() at compilation it is asked to the class B " hey class B, do you have a method M" B says yes I have. Where it is? In my base class A.

Then at runtime: "B do you override method M " , B: "Yes I do"... INVOKE override

When you ask C at compile time do you have a method named M, it says "yes, in base class A" At runtime have you override M, C says "No", OK Invoke the M in your base class

Upvotes: 0

CB Bailey
CB Bailey

Reputation: 792497

In C++ a function in a derived class with the same name and taking the same parameters (including the cv-qualification of the this parameter) as a function declared virtual in a direct or indirect base class always overrides the base class function.

The overriding function must have the same return type (or a covariant return type), otherwise the declaration of the overriding function is invalid.

If the function parameters do not match any base class function (virtual or not) with the same name is hidden by the derived class' function definition.

(Technically, an overriding function also hides the base class function that it overrides but that's a purely academic point.)

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477338

In C++, a member function in a derived class with the same signature (return type, arguments, and CV/ref qualifiers) as a virtual function of the same name in a base class overrides that function; if the signatures differ or the base class function is not virtual, then the base class function is hidden. (Although there's a slight modification to this rule involving covariant return types.)

In C++11 you can explicitly request that a member function in a derived class be an override by using the override keyword, and you can request that a base class function cannot be overridden (using the final keyword), which causes compile-time errors if you attempt to override or hide accidentally.

Upvotes: 2

Related Questions