Home c++ virtual and override

virtual and override




Looking through various examples (one of them: Lecture. Virtual functions and polymorphism ), saw that when using virtual it is possible to override a derived class method. I also saw that even if you refer to the original class, the method overridden in the child class will still be called, only if the method scope is not specified.

But (!) I dig a bunch of C++ sources and see that when a method is overridden in a child class, the word override is added. However, in the above example, no one writes override .

Is he needed or not? And in general, what does it give, this override ? Everywhere it is written that it indicates that the method is overridden, but in the examples no one uses it, and in the source code I meet it strictly all the time.

Help me to understand more specifically, virtual and override in C++ are always used only in pairs or not always? And if not always paired, how does this affect code execution at all?

I’m just writing code and I get an error reading the zero address. I think I screwed up something with these virtual methods, although I do everything according to the template.

Answer 1, authority 100%

virtual is used in the base class to make a function virtual (polymorphic).

struct A {
 void f (); // regular function
struct B: A {
 virtual void f (); // virtual function (overrides the usual A :: f)
 virtual void g () = 0; // purely virtual function

Also virtual can, but not necessarily be used in descendant classes

struct C1: B {
 void f (); // virtual, because B :: f virtual
 void g () = 0; // still a pure virtual function
struct C2: B {
 virtual void f (); // virtual, because B :: f virtual
 virtual void g () = 0; // still a pure virtual function

override is used in a descendant class to indicate that a function should override a virtual function declared in the base class. This allows you to get rid of errors when, due to a typo, instead of overriding an existing virtual function, a new one (with a different name or signature) was created.

struct C3: B {
 void f () override; // guaranteed redefinition of B :: f
 void g () override = 0;

It is allowed but not recommended to use virtual in conjunction with override .

struct C4: B {
 // virtual doesn't make sense, because override implies virtual.
 virtual void f () override;

In addition to override , there is the final specifier, which prohibits overriding a virtual function in descendant classes.

struct C5: B {
 virtual void f () final;

Answer 2, authority 37%

override seems to tell the compiler – look for this function in the ancestor classes above, it is virtual. (will not find – will give an error) Without this word, the compiler, not finding the function from the ancestors, will define it as a new function and there will be no error

Answer 3, authority 20%

virtual and override are keywords used to add methods to the virtual method table (coordinating table). This is the mechanism used in programming languages ​​to support dynamic matching.

Typically, the compiler creates a separate vtable for each class. Once an object is created, a pointer to that vtable, called a virtual table pointer or vpointer , is added as a hidden member of that object (and often the first member). The compiler also generates “hidden” code in the constructor of each class to initialize the vpointers of its objects with the addresses of the corresponding vtable. The object coordinate table contains the addresses of the dynamically linked methods of the object. The method is called when the method address is fetched from the table.

Answer 4, authority 17%

When we declare a function virtual using virtual , it is the same as if we were accessing it not directly, but through a pointer.

typedef void (* functionPointer) (ClassA *);
struct ClassA {
  functionPointer func1;
  bool called;
void callMe (ClassA * thisA) {thisA- & gt; called = true; }
ClassA ca;
ca.called = false;
ca.func1 = callMe;
ca.func1 (& amp; ca); // The callMe () function will be called.

Therefore, when accessing the overridden method, the “latest” version of the function will always be called. Even if you refer to it by specifying an ancestor class.

By using override , you can avoid accidental inheritance behavior in your code. The following example shows how accidental behavior of a member function of a derived class can occur without using override .

The compiler throws no error when using this code.

class BaseClass
  virtual void funcA ();
  virtual void funcB () const;
  virtual void funcC (int = 0);
  void funcD ();
class DerivedClass: public BaseClass
  virtual void funcA (); // ok, works as intended
  virtual void funcB (); // DerivedClass :: funcB () is not const, so it doesn't override
             // BaseClass :: funcB () const and is a new method
  virtual void funcC (double = 0.0); // DerivedClass :: funcC (double) has a different
                   // parameter type than BaseClass :: funcC (int), so
                   // DerivedClass :: funcC (double) is also a new method

More details can be found in here .

Answer 5

The override feature has been introduced in C++ since C++ 11. Your lectures and examples in them were most likely originally compiled before that time and have not been updated since then.

As you yourself [now] understand, override is just a self-checker; the use of override is optional, i.e. the performance of the earlier code is not affected in any way by the appearance of override .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions