2

i wanna know can i override non virtual function in c++ because i found this problem when playing with c++ override keyword i have the code as follows

class A
{
public:
    void say()
    {
        cout << "From A\n";
    }
};
class B : public A {
public:
    void say()
        override
    {
        cout << "From B\n";
    }
};

but when i execute the code visual studio display the following error
'B::say': method with override specifier 'override' did not override any base class methods but when i used virtual keyword in class A then error gone and code runs perfectly

Daniyal
  • 37
  • 6
  • 1
    That you can't do it is a pretty strong indicator that you can't, isn't it?. You might want to pick something from [the book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jun 28 '20 at 11:49
  • You've stumbled upon one of the most practical implications of the difference between *virtual* and *non-virtual* class members. Are you by any chance more familiar with Java? In the latter, all methods are virtual by default. – amiasato Jun 28 '20 at 12:16
  • Does this answer your question? [Why Should not Override Non-Virtual Functions](https://stackoverflow.com/questions/30679818/why-should-not-override-non-virtual-functions) –  Aug 23 '21 at 09:13

3 Answers3

5

You do not override say in B

from C++ override specifier :

In a member function declaration or definition, override ensures that the function is virtual and is overriding a virtual function from a base class. The program is ill-formed (a compile-time error is generated) if this is not true.

Look at that example :

#include <iostream>

class A
{
  public:
    void say()
    {
        std::cout << "From A\n";
    }
};

class B : public A {
  public:
    void say()
        //override
    {
        std::cout << "From B\n";
    }
};

int main()
{
  A a;
  B b;
  
  a.say();
  b.say();
  ((A &) b).say();
}

Compilation and execution :

pi@raspberrypi:/tmp $ g++ c.cc
pi@raspberrypi:/tmp $ ./a.out
From A
From B
From A
pi@raspberrypi:/tmp $ 

Putting say virtual in A (so implicitely in B) ((A &) b).say(); prints From B because that time there is overriding

bruno
  • 32,421
  • 7
  • 25
  • 37
1

No you cannot. "In a member function declaration or definition, override ensures that the function is virtual and is overriding a virtual function from a base class. The program is ill-formed (a compile-time error is generated) if this is not true."

Also see the answer here

fxweidinger
  • 173
  • 2
  • 11
  • Thanks for your answer i just got confused by watching answers on various websites like [programiz](https://www.programiz.com/cpp-programming/function-overriding#:~:text=If%20you%20create%20an%20object,is%20known%20as%20function%20overriding.) and [javatpoint](https://www.javatpoint.com/cpp-function-overriding) and many others.I am not understanding how they can say the examples they provided are the examples of overriding so i just put **override** keyword becuase i know its gonna say thats not overriding as it is developed to detect weather the function is being overritten or not – Daniyal Jun 29 '20 at 11:45
0

override does not mean that the function you mark will magically override the base function. It's purpose is to tell the compiler "please check that I'm actually overriding a base class virtual function here and give me a nice compiler error if I screwed up, instead of doing something I did not intend".

If the base class function is virtual you don't need to say override, but you can do so to help yourself make fewer mistakes.

If the base class function is not virtual you cannot override it, override keyword or not.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Thanks for your answer i just got confused by watching answers on various websites like [programiz](https://www.programiz.com/cpp-programming/function-overriding#:~:text=If%20you%20create%20an%20object,is%20known%20as%20function%20overriding.) and [javatpoint](https://www.javatpoint.com/cpp-function-overriding) and many others.I am not understanding how they can say the examples they provided are the examples of overriding so i just put **override** keyword becuase i know its gonna say thats not overriding as it is developed to detect weather the function is being overritten or not – Daniyal Jun 29 '20 at 11:44