Pure virtual and override function (c++)

Silouane

I looked over the internet for an answer to my questions but couldn't find any, so here I am.

Is it correct to specify override to my function that derived from a pure virtual:

class baseClass
{
    public:
        virtual void myFunction() = 0;
}

class derivedClass : public baseClass
{
    public:
        virtual void myFunction() override;
}

Is this correct?

And my second question is: Do I have to specify virtual in the derivedClass for my function even though no class will inherite from my derived class (it will be final)?

Thank you a lot for your answers!

Sambuca

Is this correct?

Yes. Override ensures that the function is virtual and overriding a virtual function from the base class. The program is ill-formed (a compile-time error is generated) if this is not true.

Do I have to specify virtual in the derivedClass for my function even though no class will inherite from my derived class (it will be final)?

No you don't. But even if you leave away the virtual specifier it remains virtual. Because it has been declared virtual in your BaseClass.

If some member function vf is declared as virtual in a class Base, and some class Derived, which is derived, directly or indirectly, from Base, has a declaration for member function with the same

  • name
  • parameter type list (but not the return type)
  • cv-qualifiers
  • ref-qualifiers

Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration). Base::vf does not need to be visible (can be declared private, or inherited using private inheritance) to be overridden.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Is it valid to override virtual function with pure specifier?

From Dev

Is it valid to override virtual function with pure specifier?

From Dev

C++ override pure virtual method with pure virtual method

From Dev

Can I override a virtual function with a pure virtual one?

From Dev

C++ virtual function override

From Dev

C++ virtual function override

From Java

Is there any point in using `override` when overriding a pure virtual function?

From Dev

Override pure virtual function from derived template class

From Dev

override pure virtual function not possible with const struct timepec*

From Dev

Is there a pure virtual function in the C++ Standard Library?

From Dev

Pure virtual function implementation

From Dev

C++ Abstract Base Class with pure virtual operator+ function

From Dev

Adding a body to a pure virtual/abstract function in C++?

From Dev

use of pure virtual function defined outside the class in C++?

From Dev

Performance variability of C++ pure virtual function calls

From Dev

C++ 11 Threads, Error Pure virtual function called

From Dev

Avoiding "Pure Virtual Function Call" in Derived Class C++

From Dev

SFML C++ drawing into RenderWindow pure virtual function runtime failure

From Dev

C++ passing base type to pure virtual function

From Dev

Adding a body to a pure virtual/abstract function in C++?

From Dev

C++: Avoiding repetition by calling pure virtual function

From Dev

use of pure virtual function defined outside the class in C++?

From Dev

C++ syntax of template class with pure virtual function?

From Dev

C++: Overriding virtual pure function derived from a template class

From Dev

Pure virtual function overridding virtual function

From Dev

Pure virtual function overridding virtual function

From Dev

Deriving implementation of pure virtual function

From Dev

How to implement pure virtual function

From Dev

Purpose of private pure virtual function?

Related Related

  1. 1

    Is it valid to override virtual function with pure specifier?

  2. 2

    Is it valid to override virtual function with pure specifier?

  3. 3

    C++ override pure virtual method with pure virtual method

  4. 4

    Can I override a virtual function with a pure virtual one?

  5. 5

    C++ virtual function override

  6. 6

    C++ virtual function override

  7. 7

    Is there any point in using `override` when overriding a pure virtual function?

  8. 8

    Override pure virtual function from derived template class

  9. 9

    override pure virtual function not possible with const struct timepec*

  10. 10

    Is there a pure virtual function in the C++ Standard Library?

  11. 11

    Pure virtual function implementation

  12. 12

    C++ Abstract Base Class with pure virtual operator+ function

  13. 13

    Adding a body to a pure virtual/abstract function in C++?

  14. 14

    use of pure virtual function defined outside the class in C++?

  15. 15

    Performance variability of C++ pure virtual function calls

  16. 16

    C++ 11 Threads, Error Pure virtual function called

  17. 17

    Avoiding "Pure Virtual Function Call" in Derived Class C++

  18. 18

    SFML C++ drawing into RenderWindow pure virtual function runtime failure

  19. 19

    C++ passing base type to pure virtual function

  20. 20

    Adding a body to a pure virtual/abstract function in C++?

  21. 21

    C++: Avoiding repetition by calling pure virtual function

  22. 22

    use of pure virtual function defined outside the class in C++?

  23. 23

    C++ syntax of template class with pure virtual function?

  24. 24

    C++: Overriding virtual pure function derived from a template class

  25. 25

    Pure virtual function overridding virtual function

  26. 26

    Pure virtual function overridding virtual function

  27. 27

    Deriving implementation of pure virtual function

  28. 28

    How to implement pure virtual function

  29. 29

    Purpose of private pure virtual function?

HotTag

Archive