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

Devesh Agrawal

A pure virtual function isn't supposed to have a body, but I just noticed that the following code is accepted by the compiler:

class foo
{
    virtual void dummy() = 0
    {
        cout << "hello";
    }
};

So, why are pure virtual functions allowed to have a body? Also, even when the function has a body, the class still can't be instantiated, why is that?

6502

Pure virtual function can have a body, but the fact that you declare them pure virtual is exactly to say that a derived implementation is required.

You can execute the pure virtual method from a derived method (using an explicit BaseClass::method()) but still you have to provide an implementation.

Not being able to instantiate a class with a pure virtual method that has not been overriden is the main point of the pure virtual declaration. In other words the idea of declaring a method pure virtual is to ensure that the programmer will not forget about providing its implementation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Benchmarking a pure C++ function

From Dev

Vuejs pass arguments from body to function like pure JS

From Dev

Pure virtual and override function (c++)

From Dev

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

From Java

Is this a pure function?

From Dev

Snake Game... Adding snake body function malfunctioning

From Dev

C# Recursive function adding

From Dev

Pure C function calling Objective-C method?

From Dev

Const variable in C++ function body

From Dev

Clarify about function body / C++

From Dev

C++ function Main already has a body?

From Dev

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

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

Compiling and Linking pure C and CUDA code [warning: implicit declaration of function]

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

Adding DIV to iframe body

From Dev

Adding "=" on ajax request body

From Dev

Adding classes to 'body' with JavaScript

From Dev

Is this a pure function in Scala

From Dev

Modify an argument in a pure function

Related Related

  1. 1

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

  2. 2

    Benchmarking a pure C++ function

  3. 3

    Vuejs pass arguments from body to function like pure JS

  4. 4

    Pure virtual and override function (c++)

  5. 5

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

  6. 6

    Is this a pure function?

  7. 7

    Snake Game... Adding snake body function malfunctioning

  8. 8

    C# Recursive function adding

  9. 9

    Pure C function calling Objective-C method?

  10. 10

    Const variable in C++ function body

  11. 11

    Clarify about function body / C++

  12. 12

    C++ function Main already has a body?

  13. 13

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

  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

    Compiling and Linking pure C and CUDA code [warning: implicit declaration of function]

  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

    Adding DIV to iframe body

  26. 26

    Adding "=" on ajax request body

  27. 27

    Adding classes to 'body' with JavaScript

  28. 28

    Is this a pure function in Scala

  29. 29

    Modify an argument in a pure function

HotTag

Archive