Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, is a function automatically virtual if it overrides a virtual function?

Tags:

I would expect that if foo is declared in class D, but not marked virtual, then the following code would call the implementation of foo in D (regardless of the dynamic type of d).

D& d = ...; d.foo(); 

However, in the following program, that is not the case. Can anyone explain this? Is a method automatically virtual if it overrides a virtual function?

#include <iostream>  using namespace std;  class C { public:         virtual void foo() { cout << "C" << endl; } };  class D : public C { public:         void foo() { cout << "D" << endl; } };  class E : public D { public:         void foo() { cout << "E" << endl; } };  int main(int argc, char **argv) {         E& e = *new E;         D& d = *static_cast<D*>(&e);         d.foo();         return 0; } 

The output of the above program is:

E 
like image 920
Julian Avatar asked Sep 10 '09 11:09

Julian


People also ask

Can a function be virtual and override?

Because virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual . The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual.

Should override function be virtual?

When you override a function you don't technically need to write either virtual or override . The original base class declaration needs the keyword virtual to mark it as virtual. In the derived class the function is virtual by way of having the ¹same type as the base class function.

Can you override without virtual?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

What is virtual overriding?

The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class.


2 Answers

Standard 10.3.2 (class.virtual) says:

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides*

[Footnote: A function with the same name but a different parameter list (clause over) as a virtual function is not necessarily virtual and does not override. The use of the virtual specifier in the declaration of an overriding function is legal but redundant (has empty semantics). Access control (clause class.access) is not considered in determining overriding. --- end foonote]

like image 194
Tadeusz Kopec for Ukraine Avatar answered Oct 03 '22 18:10

Tadeusz Kopec for Ukraine


Quick answer may be no, but correct answer is yes

C++ doesn't know about function hiding, so overriding virtual function without virtual keyword marks that function virtual too.

like image 42
nothrow Avatar answered Oct 03 '22 19:10

nothrow